none
How to write elements of an array to xml file RRS feed

  • Question

  • I have 3 classes as the following:

        class Bank
        {
            public Customer[] customers;
    ...
    }

        class Customer
        {
            private int customerId;
            public string customerName;
            public string customerTelephone;
            public string Address;
            public int activeaccount;
            private Account[] array;
    ...
    }

     abstract class Account {
            private int accountNumber;
            public double balance;
            protected DateTime accountOpened;
            protected DateTime accountClosed;
    ...
            }

    I would like to write these account informations and customers to an xml file. How can I write the elements of "customer" array and "account" array to xml file? How can I use Xmlwriter for these arrays?
    Sunday, February 1, 2009 11:22 PM

Answers

  •  Without declaration and namespace (but valid) it looks like:

    XDocument xmlBank =  
        new XDocument (  
            new XElement("customers",  
                    from c in customers  
                    orderby c.customerName  
                    select new XElement("customer",  
                            new XAttribute("customerID",c.customerID),  
                            new XAttribute("customerName", c.customerName),  
                            //...  
                            new XElement("accounts",  
                                from a in c.Accounts  
                                select new XElement("account",  
                                        new XElement("id", a.accountID),  
                                        new XElement("balance", a.balance),  
                                        //...  
                                            ))))); 
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:48 AM
    Monday, February 2, 2009 12:36 AM
    Moderator
  • Hi,

    This Blog should work for your scenario:

    http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=236

    cheers
    Binze
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:48 AM
    Wednesday, February 4, 2009 4:41 AM

All replies

  •  Without declaration and namespace (but valid) it looks like:

    XDocument xmlBank =  
        new XDocument (  
            new XElement("customers",  
                    from c in customers  
                    orderby c.customerName  
                    select new XElement("customer",  
                            new XAttribute("customerID",c.customerID),  
                            new XAttribute("customerName", c.customerName),  
                            //...  
                            new XElement("accounts",  
                                from a in c.Accounts  
                                select new XElement("account",  
                                        new XElement("id", a.accountID),  
                                        new XElement("balance", a.balance),  
                                        //...  
                                            ))))); 
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:48 AM
    Monday, February 2, 2009 12:36 AM
    Moderator
  • // please note, I can't remember the actual usings, but autocomplete should do it for you in VS  
     
    using System.XML.Serialization;  
     
    ...  
     
    MyClassMain myClass = new MyClassMain();  
     
    // write the class to the file  
    using(StreamWriter myWriter = new StreamWriter("MySettings.ini")) // or .txt or whatever you want, it'll output a text file in the contents of the file anyway  
    {  
      XMLSerializer myXML = new XMLSerializer(typeof(myClass));  
      myXML.serialize(myWriter, myClass);  
    }  
     
    // read the data from the file and put into the class  
    using(StreamReader myReader = new StreamReader("MySettings.ini"))  
    {  
      XMLSerializer myXML = new XMLSerializer(typeof(myClass));  
      myClass = (typeof(myClass))myXML.Deserialize(myReader);  
    }  
     
    ...  
     
    class MyClassMain  
    {  
      public string Name {get;set;}  
      public int Amount{get;set;}  

    There is more to do with what items you want saving, but that should do the very basics of outputting your whole class to a file and then retreiving it when you want.
    Monday, February 2, 2009 11:07 AM
  • Hi,

    This Blog should work for your scenario:

    http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=236

    cheers
    Binze
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:48 AM
    Wednesday, February 4, 2009 4:41 AM
  • mainstring = "peter|100|john|200|sue|300"; string[] data = mainstring.Split('|'); // Split data XElement identity = new XElement("identity"));  // Walk array of data for (int count = 0; count < data.Length; count += 2) { XElement elm = new XElement("id", new XElement("name", data[count]), new XElement("salary", data[count + 1]));   identity.Add(elm); }  XElement xml = new XElement("xml", identity);

    output of "xml" variable XElement:
    <xml>
    <identity>
    <id><name>peter</name><salary>100</salary></id>
    <id><name>john</name><salary>200</salary></id>
    <id><name>sue</name><salary>300</salary></id>
    </identity>
    </xml>
    Wednesday, March 6, 2013 8:01 AM
  • What is it you are asking here Mou_kolkata?
    Wednesday, March 6, 2013 4:43 PM
    Moderator