2

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!

Rob
  • 6,819
  • 17
  • 71
  • 131

1 Answers1

2

You can use the following XPath :

//Person[not(Surname)]
har07
  • 88,338
  • 12
  • 84
  • 137
  • Worked perfectly! :-) I used this to select just PersonID from the result: "//Person[(AdmissionNumber)]/PersonID". Thanks!! – Rob Jun 21 '15 at 00:47