XPathNodeIterator 類別

定義

提供一組選取節點的迭代器

public ref class XPathNodeIterator abstract : ICloneable, System::Collections::IEnumerable
public ref class XPathNodeIterator abstract : System::Collections::IEnumerable
public ref class XPathNodeIterator abstract : ICloneable
public abstract class XPathNodeIterator : ICloneable, System.Collections.IEnumerable
public abstract class XPathNodeIterator : System.Collections.IEnumerable
public abstract class XPathNodeIterator : ICloneable
type XPathNodeIterator = class
    interface IEnumerable
    interface ICloneable
type XPathNodeIterator = class
    interface IEnumerable
type XPathNodeIterator = class
    interface ICloneable
type XPathNodeIterator = class
    interface ICloneable
    interface IEnumerable
Public MustInherit Class XPathNodeIterator
Implements ICloneable, IEnumerable
Public MustInherit Class XPathNodeIterator
Implements IEnumerable
Public MustInherit Class XPathNodeIterator
Implements ICloneable
繼承
XPathNodeIterator
實作

範例

下列範例使用 Select 類別的 XPathNavigator 方法,以使用 XPathNodeIterator 類別選取節點集。

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XPathNodeIterator^ nodes = navigator->Select("/bookstore/book");
nodes->MoveNext();
XPathNavigator^ nodesNavigator = nodes->Current;

XPathNodeIterator^ nodesText = nodesNavigator->SelectDescendants(XPathNodeType::Text, false);

while (nodesText->MoveNext())
    Console::WriteLine(nodesText->Current->Value);
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

while (nodesText.MoveNext())
    Console.WriteLine(nodesText.Current.Value);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim nodes As XPathNodeIterator = navigator.Select("/bookstore/book")
nodes.MoveNext()
Dim nodesNavigator As XPathNavigator = nodes.Current

Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While

該範例採用 books.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>  

備註

XPathNodeIterator類別所 XPathNavigator 傳回的物件不會位於所選節點集中的第一個節點上。 必須呼叫 MoveNext 類別的 XPathNodeIterator 方法,以將物件放在 XPathNodeIterator 所選節點集中的第一個節點上。

使用 XPathNodeIterator 時,如果您編輯目前的節點或其任何上階,則目前的位置會遺失。 如果您想要編輯已選取的節點數目,請建立 XPathNavigator 陣列,將所有節點從 XPathNodeIterator 複製到陣列,然後逐一查看陣列並修改節點。

有兩種方式可以使用 類別逐一 XPathNavigator 查看集合 XPathNodeIterator

其中一種方式是使用 MoveNext 方法,然後呼叫 Current 以取得目前的 XPathNavigator 實例,如下列範例所示:

while (nodeIterator->MoveNext())
{
    XPathNavigator^ n = nodeIterator->Current;
Console::WriteLine(n->LocalName);
}
while (nodeIterator.MoveNext())
{
    XPathNavigator n = nodeIterator.Current;
    Console.WriteLine(n.LocalName);
}
While nodeIterator.MoveNext()
    Dim n As XPathNavigator = nodeIterator.Current
    Console.WriteLine(n.LocalName)
End While

另一 foreach 種方式是使用 迴圈來呼叫 GetEnumerator 方法,並使用傳 IEnumerator 回的介面來列舉節點,如下列範例所示:

for each (XPathNavigator^ n in nodeIterator)
Console::WriteLine(n->LocalName);
foreach (XPathNavigator n in nodeIterator)
    Console.WriteLine(n.LocalName);
For Each n As XPathNavigator In nodeIterator
    Console.WriteLine(nav.LocalName)
Next

您應該使用 MoveNextCurrentGetEnumerator 或使用 。 結合這兩種方法可能會導致非預期的結果。 例如,如果 MoveNext 先呼叫 方法,然後在 GetEnumerator 迴圈中 foreach 呼叫 方法, foreach 迴圈就不會開始列舉集合開頭的結果,而是從 方法之後 Current 的位置開始列舉結果。

給實施者的注意事項

當您繼承自 類別時 XPathNodeIterator ,必須覆寫下列成員:

建構函式

XPathNodeIterator()

初始化 XPathNodeIterator 類別的新執行個體。

屬性

Count

取得選取節點集中最後一個節點的索引。

Current

在衍生類別中覆寫時,取得這個 XPathNodeIteratorXPathNavigator 物件,該物件位於目前的內容節點上。

CurrentPosition

在衍生類別中覆寫時,取得選取節點集中目前位置的索引。

方法

Clone()

在衍生類別中覆寫時,傳回這個 XPathNodeIterator 物件的複製。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回 IEnumerator 物件,逐一查看選取的節點集。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MoveNext()

在衍生類別中覆寫時,將 Current 屬性所傳回的 XPathNavigator 物件移至選取節點集內的下一個節點。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICloneable.Clone()

建立目前執行個體複本的新物件。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於