26

Given this xml doc

<listOfItem>
  <Item id="1"> 
    <attribute1 type="foo"/>
    <attribute2 type="bar"/>
    <property type="x"/>
    <property type="y"/>
    <attribute3 type="z"/>
  </Item>
  <Item>
   //... same child nodes
  </Item>
 //.... other Items
</listOfItems>

Given this xml document, I would like to select, for each "Item" node, just the "property" child nodes. How can I do it in c# directly? With "directly" I mean without selecting all the child nodes of Item and then check one by one. So far:

XmlNodeList nodes = xmldoc.GetElementsByTagName("Item");
foreach(XmlNode node in nodes)
{
   doSomething()
   foreach(XmlNode child in node.ChildNodes)
   {
     if(child.Name == "property")
     {
        doSomethingElse()
     }
   }
}
venerik
  • 5,766
  • 2
  • 33
  • 43
accand
  • 531
  • 1
  • 8
  • 17

2 Answers2

39

You can use SelectNodes(xpath) method instead of ChildNodes property:

foreach(XmlNode child in node.SelectNodes("property"))
{
    doSomethingElse()
}

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Wait a second, this select all the "property" nodes at once, I am trying to select onlt the ones child of a current "Item" node. – accand May 21 '15 at 17:03
  • 1
    @user2567853 Oops, that's because I put `//` in front of `property`. Removing the double-slash should fix this. – Sergey Kalinichenko May 21 '15 at 17:25
  • 1
    Just a side note(not what OP asked): One can use `node.SelectSingleNode("property")` if you have only 1 child node. – checksum Jan 29 '20 at 07:00
9

Try using LINQ to XML instead of XML DOM as it's much simpler syntax for what you want to do.

XDocument doc = XDocument.Load(filename);
foreach (var itemElement in doc.Element("listOfItems").Elements("Item"))
{
   var properties = itemElement.Elements("property").ToList();
}
toadflakz
  • 7,764
  • 1
  • 27
  • 40
  • 1
    Thank you for your answer, it seems very nice, but at this moment I am a real newbie and I feel more familiar with the other solution proposed. But I'll keep this mind for other works... – accand May 21 '15 at 09:54