I have this xml document:
<?xml version="1.0" ?>
<object>
<name>Sphere</name>
<material>Steel</material>
<device Id="01">
<model>Model 1</model>
<color>Red</color>
</device>
<device Id="02">
<model>Model 2</model>
<color>Blue</color>
</device>
</object>
I want to be able to read in a for loop both the model and the color of each device. My code can only read only one value at a time (either model or value) and I have to go through the loop twice.
I expect there should be a more elegant solution.
var xDoc = XDocument.Load(@"C:\_Projects\AProjectsCS\XML_Tutorial\Sample.xml");
IEnumerable<XElement> list1 = xDoc.Root.Descendants("model");
IEnumerable<XElement> list2 = xDoc.XPathSelectElements("//color");
foreach (XElement el in list1)
Console.WriteLine(el.Value);
foreach (XElement el in list2)
Console.WriteLine(el.Value);
Thanks, Nick