I have an XML document that can have multiple children elements under another element, and I was wondering how I can get all these elements and store as an object? For example-
<?xml version="1.0" encoding="utf-8" ?>
<export>
<order>
<ordernumber>100</ordernumber>
<items>
<item>
<name>table</name>
</item>
<item>
<name>chair</name>
</item>
</items>
</order>
</export>
So say I get all orders using LINQ and store in a list
var xdoc = XDocument.Load(myXMLReader);
var result = (from x in xdoc.Root.Elements()
select new Order
{
OrderNumber = (string)x.Element("OrderNumber")
}).ToList();
What do I need to do to the above to get ALL items in an order and store in another object property such as a List or something similar? EG.
var result = (from x in xdoc.Root.Elements()
select new Order
{
OrderNumber = (string)x.Element("OrderNumber")
//PSUEDO CODE ADDITION
Items = (new { Name = itemname}).ToList()
// END PSUEDO CODE ADDITION
}).ToList();
[Serializable]and deserialize the XML into the type (or a proxy type from which you copy to the real type somehow). As far as processing the XML directly, have you looked at either theXContainer.Descendants()orXContainer.Elements()method?