Mega Code Archive

 
Categories / C# / XML
 

XPathNavigator Matches tells whether the current node matches the specified XPathExpression

using System; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Collections; using System.Collections.Generic; using System.Xml.XPath; public class MainClass{    public static void Main(){                      XPathDocument document = new XPathDocument("books.xml");         XPathNavigator navigator = document.CreateNavigator();                  XPathNodeIterator nodes = navigator.SelectDescendants("book", "", false);                  XPathExpression expr = navigator.Compile("book[@genre='data']");         while (nodes.MoveNext())         {             XPathNavigator navigator2 = nodes.Current.Clone();             if (navigator2.Matches(expr))             {                 navigator2.MoveToFirstChild();                 Console.WriteLine("Book title:  {0}", navigator2.Value);             }         }     } }