Прочетете на английски Редактиране

Споделяне чрез


XPathNavigator.Matches Method

Definition

Determines whether the current node matches the specified System.Xml.XPath expression.

Overloads

Matches(String)

Determines whether the current node matches the specified XPath expression.

Matches(XPathExpression)

Determines whether the current node matches the specified XPathExpression.

Matches(String)

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

Determines whether the current node matches the specified XPath expression.

C#
public virtual bool Matches(string xpath);

Parameters

xpath
String

The XPath expression.

Returns

true if the current node matches the specified XPath expression; otherwise, false.

Exceptions

The XPath expression cannot be evaluated.

The XPath expression is not valid.

Examples

For an example of the Matches method, see the XPathNavigator.Matches method.

Remarks

This method has no affect on the state of the XPathNavigator.

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Matches(XPathExpression)

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

Determines whether the current node matches the specified XPathExpression.

C#
public virtual bool Matches(System.Xml.XPath.XPathExpression expr);

Parameters

expr
XPathExpression

An XPathExpression object containing the compiled XPath expression.

Returns

true if the current node matches the XPathExpression; otherwise, false.

Exceptions

The XPath expression cannot be evaluated.

The XPath expression is not valid.

Examples

The following example displays the titles of all novels.

C#
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);
    }
}

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

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>  

Remarks

This method has no effect on the state of the XPathNavigator. This method is identical to the XPathNavigator.Matches method, except that a XPathExpression object containing the compiled XPath expression is specified, rather than an XPath expression String.

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1