XML document for ASP.NET C# project

Posted on 2012-08-24
Last Modified: 2012-08-30

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!
Question by:Rad1
    LVL 18

    Accepted Solution

    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

    Author Closing Comment

    Thank you Gary!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now