XPathNodeIterator.Current 属性


在派生类中重写时,获取此 XPathNavigatorXPathNodeIterator 对象,它定位在当前上下文节点上。When overridden in a derived class, gets the XPathNavigator object for this XPathNodeIterator, positioned on the current context node.

 abstract property System::Xml::XPath::XPathNavigator ^ Current { System::Xml::XPath::XPathNavigator ^ get(); };
public abstract System.Xml.XPath.XPathNavigator Current { get; }
member this.Current : System.Xml.XPath.XPathNavigator
Public MustOverride ReadOnly Property Current As XPathNavigator


XPathNavigator 对象,定位在从中选择节点集的上下文节点上。An XPathNavigator object positioned on the context node from which the node set was selected. 必须调用 MoveNext() 方法将 XPathNodeIterator 移至所选集中的第一个节点。The MoveNext() method must be called to move the XPathNodeIterator to the first node in the selected set.


下面的示例使用 XPathNodeIterator 对象的 Current 属性和 XPathNavigator 类的 Clone 方法获取 Herman Melville 创作的所有书名。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.

XPathDocument^ document = gcnew 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();
    Console::WriteLine("Book title: {0}", clone->Value);
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();
    Console.WriteLine("Book title: {0}", clone.Value);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Select all books authored by Melville.
Dim nodes As XPathNodeIterator = 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.
    Dim clone As XPathNavigator = nodes.Current.Clone()
    Console.WriteLine("Book title: {0}", clone.Value)
End While

该示例使用 contosoBooks.xml 文件作为输入。The example takes the contosoBooks.xml file as input.

<?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>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  


您可以使用返回 XPathNavigator 对象的属性来获取有关当前节点的信息。You can use the properties of the returned XPathNavigator object to obtain information on the current node. 但是,不应修改返回的 XPathNavigator 对象。However, the returned XPathNavigator object should not be modified. 返回的 XPathNavigator 对象无法从所选节点集中移走。The returned XPathNavigator object cannot be moved away from the selected node set.

或者,您可以使用 XPathNavigator 类的 Clone 方法克隆 XPathNavigator 对象。Alternatively, you can clone the XPathNavigator object using the Clone method of the XPathNavigator class. 然后,可以将克隆的 XPathNavigator 对象移出所选节点集。The cloned XPathNavigator object can then be moved away from the selected node set. 这种克隆 XPathNavigator 对象的方法可能会影响 XPath 查询的性能。This method of cloning the XPathNavigator object might affect the performance of the XPath query.

如果 SelectAncestorsSelectDescendantsSelectChildren 方法导致未选择节点,则 Current 属性可能未指向上下文节点。If the SelectAncestors, SelectDescendants, and SelectChildren methods result in no nodes being selected, the Current property might not be pointing to the context node.

若要测试是否已选择节点,请使用 Count 属性,如下面的示例中所示。To test whether nodes have been selected, use the Count property as shown in the following example.