• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

XML document for ASP.NET C# project


How do I create an xml document in VS2010 in ASP.NET 4.0 C# project and save array content in it to use later on when needed?  I will be needing to get the numbers back from the XML document and populate the array when needed.

Would you please point me to a sample code if possible!

Thank you!
1 Solution
Gary DavisDir Internet SvcsCommented:
Let's assume your array is an array of a class Car. First create the xml root that will contain the array elements (Inventory in this example). Next, create the Car elements for each entry in the array:

    class Car
        public string OwnerName;
        public int ID;

    static void CreateXmlDocFromArray()
        // Create an anonymous array of types.           
        var data = new[]
            new Car {OwnerName = "Abhisek", ID = 10},
            new Car {OwnerName = "Satish", ID = 11},
            new Car {OwnerName = "Mukesh", ID = 12},
            new Car {OwnerName = "Ranjan", ID = 13}

        // Now enumerate over the array to build an XElement.            
        XElement vehicles =
            new XElement("Inventory",
                from c in data
                select new XElement("Car",
                    new XAttribute("ID", c.ID),
                    new XElement("OwnerName", c.OwnerName)

Open in new window

The resulting xml is:

  <Car ID="10">
  <Car ID="11">
  <Car ID="12">
  <Car ID="13">

Open in new window

This comes from this web posting:


Gary Davis
Rad1Author Commented:
Thank you Gary!!!

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now