Search code examples
.netxmlxpathsiblings

Count parent nodes which have 2 types of child nodes - specifically 1 "boy" node and 1 "girl" node each


I have XML files containing data like this...

    <parent>
        <boy/>
    </parent>
    <parent>
        <girl/>
    <parent>
    <parent>
        <boy/>
        <girl/>
    </parent>

...and I want to count the parents who have both types of child nodes IE: 1 boy and 1 girl each. In the above example the answer is 1 as only 1 parent has both a boy and a girl. I'm using xpath to query the XML. What I currently have is something like this...

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(inXML);
 int ParentsWithBothKids = xmlDoc.SelectNodes("MY QUERY HERE!!!").Count;

The various queries I try either throw an error as being invalid xpath or always return 0 even though the small test XML file I'm using definitely has some.

Any ideas as to what xpath query I can use?


Solution

  • Try parent[boy and girl] as your xpath query.

    Check out XPath Examples on MSDN.