Export (0) Print
Expand All

XPathNavigator.Matches Method

Determines whether the current node matches the specified XSLT pattern.

Overload List

Determines whether the current node matches the specified XSLT pattern.

[Visual Basic] Overloads Public Overridable Function Matches(String) As Boolean
[C#] public virtual bool Matches(string);
[C++] public: virtual bool Matches(String*);
[JScript] public function Matches(String) : Boolean;

Determines whether the current node matches the specified XPathExpression.

[Visual Basic] Overloads Public Overridable Function Matches(XPathExpression) As Boolean
[C#] public virtual bool Matches(XPathExpression);
[C++] public: virtual bool Matches(XPathExpression*);
[JScript] public function Matches(XPathExpression) : Boolean;

Example

The following example displays the titles of all novels.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Matches. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Xml.XPath

public class Sample

  public shared sub Main()

    Dim doc as XPathDocument = new XPathDocument("books.xml")
    Dim nav as XPathNavigator = doc.CreateNavigator()
 
    ' Select all book nodes.
    Dim ni as XPathNodeIterator = nav.SelectDescendants("book", "", false)

    ' Select all book nodes that have the matching attribute value.
    Dim expr as XPathExpression = nav.Compile("book[@genre='novel']")
    while (ni.MoveNext()) 
       Dim nav2 as XPathNavigator = ni.Current.Clone()
       if (nav2.Matches(expr))
         nav2.MoveToFirstChild()
         Console.WriteLine("Book title:  {0}", nav2.Value)
       end if
    end while
  end sub
end class

[C#] 
using System;
using System.Xml.XPath;

public class Sample
{
  public static void Main()
  {

    XPathDocument doc = new XPathDocument("books.xml");
    XPathNavigator nav = doc.CreateNavigator();
 
    // Select all book nodes.
    XPathNodeIterator ni = nav.SelectDescendants("book", "", false);

    // Select all book nodes that have the matching attribute value.
    XPathExpression expr = nav.Compile("book[@genre='novel']");
    while (ni.MoveNext()) {
       XPathNavigator nav2 = ni.Current.Clone();
       if (nav2.Matches(expr)){
         nav2.MoveToFirstChild();
         Console.WriteLine("Book title:  {0}", nav2.Value);
       }
    }
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml::XPath;

int main()
{
   XPathDocument* doc = new XPathDocument(S"books.xml");
   XPathNavigator * nav = doc -> CreateNavigator();

   // Select all book nodes.
   XPathNodeIterator * ni = nav -> SelectDescendants(S"book", S"", false);

   // Select all book nodes that have the matching attribute value.
   XPathExpression * expr = nav -> Compile(S"book->Item[@genre='novel']");
   while (ni -> MoveNext())
   {
      XPathNavigator * nav2 = ni -> Current -> Clone();
      if (nav2 -> Matches(expr))
      {
         nav2 -> MoveToFirstChild();
         Console::WriteLine(S"Book title: {0}", nav2 -> Value);
      }
   }
}

The example uses the file, books.xml, as input.

<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XPathNavigator Class | XPathNavigator Members | System.Xml.XPath Namespace

Show:
© 2014 Microsoft