共用方式為


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 ,取得 Herman Melville 所撰寫的所有書籍標題。

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 物件 CloneXPathNavigator然後,複製的物件就可以從選取的節點集移開。 複製物件的這個方法 XPathNavigator 可能會影響 XPath 查詢的效能。

SelectAncestors如果 、 SelectDescendantsSelectChildren 方法導致未選取任何節點, Current 則 屬性可能不會指向內容節點。

若要測試是否已選取節點,請使用 Count 屬性,如下列範例所示。

適用於

另請參閱