XPathNodeIterator.Current 属性

定义

在派生类中重写时,获取此 XPathNodeIteratorXPathNavigator 对象,它定位在当前上下文节点上。

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

属性值

XPathNavigator

XPathNavigator 对象,定位在从中选择节点集的上下文节点上。 必须调用 MoveNext() 方法将 XPathNodeIterator 移至所选集中的第一个节点。

示例

以下示例使用 Current 对象的属性 XPathNodeIteratorClone 类的方法 XPathNavigator 获取赫曼·梅尔维尔创作的所有书名。

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();
    clone->MoveToFirstChild();
    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();
    clone.MoveToFirstChild();
    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()
    clone.MoveToFirstChild()
    Console.WriteLine("Book title: {0}", clone.Value)
End While

该示例使用 contosoBooks.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>  

注解

可以使用返回 XPathNavigator 对象的属性来获取当前节点上的信息。 但是,不应修改返回 XPathNavigator 的对象。 返回 XPathNavigator 的对象不能从所选节点集移开。

或者,可以使用类的方法XPathNavigator克隆XPathNavigator对象Clone。 然后,可以将克隆 XPathNavigator 的对象移离所选节点集。 克隆 XPathNavigator 对象的此方法可能会影响 XPath 查询的性能。

SelectAncestors如果 ,SelectDescendantsSelectChildren方法导致未选择任何节点,则Current属性可能不指向上下文节点。

若要测试是否已选择节点,请使用 Count 如以下示例所示的属性。

适用于

另请参阅