XPath query to select nodes

262 views Asked by At

I have the following XML:

   <?xml version="1.0" encoding="utf-8"?>
    <Persons>
      <Person>
        <PersonID>6352</PersonID>
        <Forename>Tristan</Forename>
      </Person>
      <Person>
        <PersonID>6353</PersonID>
        <Forename>Ruth</Forename>
      </Person>
      <Person>
        <PersonID>6913</PersonID>
        <Forename>Mina</Forename>
        <Surname>Asif</Surname>
      </Person>
      <Person>
        <PersonID>6914</PersonID>
        <Forename>Clark</Forename>
        <Surname>Williams</Surname>
      </Person>
    </Persons>

I use the following code to load the xml:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"example.xml");

I need to ask for all nodes that DO NOT have a Surname using an XPath query and the select nodes call (something similar to the following pseudo):

XmlNodeList nodes1 = xDoc.SelectNodes("//Person[Surname=null]");

I'm pretty new to XPath, any pointers much appreciated!

1

There are 1 answers

1
har07 On BEST ANSWER

You can use the following XPath :

//Person[not(Surname)]