2015-06-21 183 views
2

我有下面的XML:XPath查詢選擇節點

<?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> 

我用下面的代碼加載XML:

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

我要問,對於不具有的所有節點使用XPath查詢和選擇節點調用(類似於下面的僞東西)姓:

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

我是很新,XPath的,任何p非常感謝!

+1

看一看http://stackoverflow.com/questions/15620469/xslt-xpath-test-for-null – lloyd

回答

2

您可以使用以下XPath:

//Person[not(Surname)] 
+0

完美地工作! :-)我用這個來從結果中選擇PersonID:「// Person [(AdmissionNumber)]/PersonID」。謝謝!! –