XPathNodeIterator.MoveNext 메서드

정의

파생 클래스에서 재정의하는 경우 XPathNavigator 속성에서 반환된 Current 개체를 선택된 노드 집합의 다음 노드로 이동합니다.

public:
 abstract bool MoveNext();
public abstract bool MoveNext ();
abstract member MoveNext : unit -> bool
Public MustOverride Function MoveNext () As Boolean

반환

Boolean

XPathNavigator 개체가 다음 노드로 이동되었으면 true이고 선택된 노드가 더 이상 없으면 false입니다.

예제

다음 예제에서는 클래스의 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 개체는 메서드에 대한 초기 호출 후에만 선택한 노드 집합의 첫 번째 노드에 MoveNext 배치됩니다. 노드 집합은 문서 순서대로 만들어집니다. 따라서 메서드를 호출하면 MoveNext 문서 순서대로 다음 노드로 이동합니다.

클래스를 사용하여 컬렉션을 반복 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

메서드를 MoveNext 사용하거나 메서드 CurrentGetEnumerator 사용해야 합니다. 이러한 두 가지 방법을 결합하면 예기치 않은 결과가 발생할 수 있습니다. 예를 들어 메서드가 MoveNext 먼저 호출된 다음 GetEnumerator 메서드가 루프 foreach 에서 foreach 호출되는 경우 루프는 컬렉션의 시작 부분과 메서드 뒤 Current 의 위치에서 결과를 열거하기 시작하지 않습니다.

적용 대상

추가 정보