Identify all the nodes which contains particular inner text- Using XPath

Yesterday I had a small issue to identify the entire node which contains particular inner text. I just rolled my head on that area… Initially I thought of iterating all the child node of the parent using SelectSingleNode() method of XML document class. Yes it was working fine.  I changed my view after that to use XPath.. got confused how to write a single Xpath query to identify the entire node which contains particular inner text. Finally I got the solution. Thought off share the idea. Here it is.

 

Sample XMl File

<Books> 
     <Book>
             <Name>ABC</Name>
    <Author>
             <Name>ABC</Name>
    </Author>
</Book>

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@”C:\Test.xml”);
 XmlNodeList Test = xmlDocument.SelectNodes(“descendant:: Books//*[contains(text(), ‘ABC’)]”);     
Console.WriteLine(Test.Count);

The xpath execution will give 2 node counts.

Advertisements