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

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


XPathNodeIterator.Current Property

Definition

When overridden in a derived class, gets the XPathNavigator object for this XPathNodeIterator, positioned on the current context node.

C#
public abstract System.Xml.XPath.XPathNavigator? Current { get; }
C#
public abstract System.Xml.XPath.XPathNavigator Current { get; }

Property Value

An XPathNavigator object positioned on the context node from which the node set was selected. The MoveNext() method must be called to move the XPathNodeIterator to the first node in the selected set.

Examples

The following example gets all book titles authored by Herman Melville using the Current property of the XPathNodeIterator object and the Clone method of the XPathNavigator class.

C#
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

// Select all books authored by Melville.
XPathNodeIterator nodes = navigator.Select("descendant::book[author/last-name='Melville']");

while (nodes.MoveNext())
{
    // Clone the navigator returned by the Current property.
    // Use the cloned navigator to get the title element.
    XPathNavigator clone = nodes.Current.Clone();
    clone.MoveToFirstChild();
    Console.WriteLine("Book title: {0}", clone.Value);
}

The example takes the contosoBooks.xml file as input.

XML
<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <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

You can use the properties of the returned XPathNavigator object to obtain information on the current node. However, the returned XPathNavigator object should not be modified. The returned XPathNavigator object cannot be moved away from the selected node set.

Alternatively, you can clone the XPathNavigator object using the Clone method of the XPathNavigator class. The cloned XPathNavigator object can then be moved away from the selected node set. This method of cloning the XPathNavigator object might affect the performance of the XPath query.

If the SelectAncestors, SelectDescendants, and SelectChildren methods result in no nodes being selected, the Current property might not be pointing to the context node.

To test whether nodes have been selected, use the Count property as shown in the following example.

Applies to

Продукт Версии
.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

See also