Share via


XPathNavigator.Matches 方法

定義

判斷目前的節點是否符合指定的 System.Xml.XPath 運算式。

多載

Matches(String)

判斷目前的節點是否符合指定的 XPath 運算式。

Matches(XPathExpression)

判斷目前節點是否符合指定的 XPathExpression

Matches(String)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

判斷目前的節點是否符合指定的 XPath 運算式。

public:
 virtual bool Matches(System::String ^ xpath);
public virtual bool Matches (string xpath);
abstract member Matches : string -> bool
override this.Matches : string -> bool
Public Overridable Function Matches (xpath As String) As Boolean

參數

xpath
String

XPath 運算式。

傳回

如果目前的節點符合指定的 XPath 運算式,則為 true,否則為 false

例外狀況

無法評估 XPath 運算式。

XPath 運算式無效。

範例

如需 方法的 Matches 範例,請參閱 XPathNavigator.Matches 方法。

備註

這個方法不會影響 的狀態 XPathNavigator

適用於

Matches(XPathExpression)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

判斷目前節點是否符合指定的 XPathExpression

public:
 virtual bool Matches(System::Xml::XPath::XPathExpression ^ expr);
public virtual bool Matches (System.Xml.XPath.XPathExpression expr);
abstract member Matches : System.Xml.XPath.XPathExpression -> bool
override this.Matches : System.Xml.XPath.XPathExpression -> bool
Public Overridable Function Matches (expr As XPathExpression) As Boolean

參數

expr
XPathExpression

包含已編譯 XPath 運算式的 XPathExpression 物件。

傳回

如果目前節點符合 XPathExpression,則為 true,否則為 false

例外狀況

無法評估 XPath 運算式。

XPath 運算式無效。

範例

下列範例會顯示所有新書的標題。

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

// Select all book nodes.
XPathNodeIterator^ nodes = navigator->SelectDescendants("book", "", false);

// Select all book nodes that have the matching attribute value.
XPathExpression^ expr = navigator->Compile("book[@genre='novel']");
while (nodes->MoveNext())
{
    XPathNavigator^ navigator2 = nodes->Current->Clone();
    if (navigator2->Matches(expr))
    {
        navigator2->MoveToFirstChild();
        Console::WriteLine("Book title:  {0}", navigator2->Value);
    }
}
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

// Select all book nodes.
XPathNodeIterator nodes = navigator.SelectDescendants("book", "", false);

// Select all book nodes that have the matching attribute value.
XPathExpression expr = navigator.Compile("book[@genre='novel']");
while (nodes.MoveNext())
{
    XPathNavigator navigator2 = nodes.Current.Clone();
    if (navigator2.Matches(expr))
    {
        navigator2.MoveToFirstChild();
        Console.WriteLine("Book title:  {0}", navigator2.Value);
    }
}
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Select all book nodes.
Dim nodes As XPathNodeIterator = navigator.SelectDescendants("book", "", False)

' Select all book nodes that have the matching attribute value.
Dim expr As XPathExpression = navigator.Compile("book[@genre='novel']")
While nodes.MoveNext()
    Dim navigator2 As XPathNavigator = nodes.Current.Clone()
    If navigator2.Matches(expr) Then
        navigator2.MoveToFirstChild()
        Console.WriteLine("Book title:  {0}", navigator2.Value)
    End If
End While

此範例會使用 檔案 books.xml ,作為輸入。

<?xml version="1.0" encoding="utf-8" ?>   
<bookstore>  
    <book genre="autobiography" publicationdate="1981-03-22" 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-11-17" 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-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

備註

這個方法不會影響 的狀態 XPathNavigator 。 這個方法與 方法相同 XPathNavigator.Matches ,不同之處在于 XPathExpression 指定包含已編譯 XPath 運算式的物件,而不是 XPath 運算式 String

適用於