Select a subset of childnodes by name
Asked Answered
I

2

28

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()
     }
   }
}
Illona answered 21/5, 2015 at 9:42 Comment(0)
G
41

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

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

Demo.

Gerontology answered 21/5, 2015 at 9:50 Comment(3)
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.Illona
@user2567853 Oops, that's because I put // in front of property. Removing the double-slash should fix this.Gerontology
Just a side note(not what OP asked): One can use node.SelectSingleNode("property") if you have only 1 child node.Elayne
K
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();
}
Knelt answered 21/5, 2015 at 9:47 Comment(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...Illona

© 2022 - 2024 — McMap. All rights reserved.