LINQ can be used to dynamically and quickly parse XML.
The following data can be read in like so:
1
2
3
4
5
6
7
8
9
10
| <root>
<Element>
<Name>.....</Name>
<Height>.....</Height>
</Element>
<Element>
<Name>.....</Name>
<Height>.....</Height>
</Element>
</root>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| XDocument doc = XDocument.Load("file.xml");
var collection = from r in doc.Descendants("Element")
select new
{
Name = r.Element("Name").Value,
Height = r.Element("Height").Value,
};
foreach(var element in collection)
{
// Properties can be accessed like an object
// element.Name;
// element.Height;
}
|
Comments
Post a Comment