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

XML document for ASP.NET C# project

HI,

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!
0
Rad1
Asked:
Rad1
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)
                )
            );
        Console.WriteLine(vehicles);
    }

Open in new window


The resulting xml is:

<Inventory>
  <Car ID="10">
    <OwnerName>Abhisek</OwnerName>
  </Car>
  <Car ID="11">
    <OwnerName>Satish</OwnerName>
  </Car>
  <Car ID="12">
    <OwnerName>Mukesh</OwnerName>
  </Car>
  <Car ID="13">
    <OwnerName>Ranjan</OwnerName>
  </Car>
</Inventory>

Open in new window


This comes from this web posting:

http://www.dotnetspark.com/kb/1164-create-xml-document-programaticly-usinh.aspx

Thanks,
Gary Davis
0
 
Rad1Author Commented:
Thank you Gary!!!
0

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