XPathNavigator.Select 메서드

정의

지정된 XPath 식을 사용하여 노드 집합을 선택합니다.

오버로드

Select(String)

지정된 XPath 식을 사용하여 노드 집합을 선택합니다.

Select(XPathExpression)

지정된 XPathExpression을 사용하여 노드 집합을 선택합니다.

Select(String, IXmlNamespaceResolver)

네임스페이스 접두사를 확인하기 위해 지정된 IXmlNamespaceResolver 개체와 함께 XPath 식을 사용하여 노드 집합을 선택합니다.

Select(String)

지정된 XPath 식을 사용하여 노드 집합을 선택합니다.

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ Select(System::String ^ xpath);
public virtual System.Xml.XPath.XPathNodeIterator Select (string xpath);
abstract member Select : string -> System.Xml.XPath.XPathNodeIterator
override this.Select : string -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function Select (xpath As String) As XPathNodeIterator

매개 변수

xpath
String

XPath 식을 나타내는 String입니다.

반환

XPathNodeIterator

선택한 노드 집합을 가리키는 XPathNodeIterator입니다.

예외

XPath 식에 오류가 포함되어 있거나 해당 반환 형식이 노드 집합이 아닙니다.

XPath 식이 잘못되었습니다.

예제

다음 예제에서는 메서드를 Select 사용하여 노드 집합을 선택합니다.

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>  

설명

선택 영역의 컨텍스트는 이 메서드를 호출할 때의 XPathNavigator 위치입니다. 이 메서드를 호출한 후 반환된 XPathNodeIterator 노드는 선택한 노드 집합을 나타냅니다. 선택한 노드 집합을 XPathNodeIterator 반복하는 메서드를 사용합니다MoveNext.

다음 C# 코드는 선택한 노드 집합을 반복합니다.

XPathNodeIterator iterator = nav.Select("/bookstore/book");  
while (iterator.MoveNext())  
{  
    Console.WriteLine(Iterator.Current.Name);  
}  

다음은 메서드를 사용할 때 고려해야 할 중요한 참고 사항입니다 Select .

추가 정보

적용 대상

Select(XPathExpression)

지정된 XPathExpression을 사용하여 노드 집합을 선택합니다.

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ Select(System::Xml::XPath::XPathExpression ^ expr);
public virtual System.Xml.XPath.XPathNodeIterator Select (System.Xml.XPath.XPathExpression expr);
abstract member Select : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNodeIterator
override this.Select : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function Select (expr As XPathExpression) As XPathNodeIterator

매개 변수

expr
XPathExpression

컴파일된 XPath 쿼리를 포함하는 XPathExpression 개체입니다.

반환

XPathNodeIterator

선택된 노드 집합을 가리키는 XPathNodeIterator입니다.

예외

XPath 식에 오류가 포함되어 있거나 해당 반환 형식이 노드 집합이 아닙니다.

XPath 식이 잘못되었습니다.

예제

다음 예제에서는 메서드를 Select 사용하여 노드 집합을 선택합니다.

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

XPathExpression^ query = navigator->Compile("/bookstore/book");
XPathNodeIterator^ nodes = navigator->Select(query);
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();

XPathExpression query = navigator.Compile("/bookstore/book");
XPathNodeIterator nodes = navigator.Select(query);
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 query As XPathExpression = navigator.Compile("/bookstore/book")
Dim nodes As XPathNodeIterator = navigator.Select(query)
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>  

설명

선택 영역의 컨텍스트는 이 메서드를 호출할 때의 XPathNavigator 위치입니다. 이 메서드를 호출하면 반환된 XPathNodeIterator 노드 집합이 표시됩니다. 선택한 노드 집합을 XPathNodeIterator 반복하는 데 사용합니다MoveNext.

다음 C# 코드는 선택한 노드 집합을 반복합니다.

XPathNodeIterator ni = nav.Select(expr);  
while (ni.MoveNext())  
{  
    Console.WriteLine(ni.Current.Name);  
}  

다음은 메서드를 사용할 때 고려해야 할 중요한 참고 사항입니다 Select .

예를 들어 문서에 다음 XML 노드가 포함되어 있다고 가정합니다.

<bookstore xmlns:bk='urn:samples'>  
    <book bk:ISBN='1-325-0980'>  
        <title>Pride And Prejudice</title>  
    </book>  
</bookstore>  

이 경우 다음 C# 코드는 노드를 bk:ISBN 선택합니다.

XPathExpression expr = nav.Compile("book/@bk:ISBN");  
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());  
mngr.AddNamespace("bk","urn:samples");  
expr.SetContext(mngr);  
XPathNodeIterator ni = nav.Select(expr);  

참고

XPathExpression 접두사에 포함되지 않은 경우 네임스페이스 URI가 빈 네임스페이스라고 가정합니다. XML에 기본 네임스페이스가 포함되어 있는 경우에도 메서드를 SetContext 사용하고 기본 네임스페이스를 처리할 접두사 및 네임스페이스 URI가 포함된 메서드를 제공해야 XmlNamespaceManager 합니다.

예를 들어 다음 XML이 있다고 가정합니다.

<bookstore xmlns="http://www.lucernepublishing.com">  
    <book>  
        <title>Pride And Prejudice</title>  
    </book>  
</bookstore>  

이 경우 다음 C# 코드는 모든 책 노드를 선택합니다.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);  
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");  
XPathExpression expr;  
expr = nav.Compile("//ab:book");  
expr.SetContext(nsmgr);  
XPathNodeIterator ni = nav.Select(expr);  

이 메서드는 XPathNavigator.

추가 정보

적용 대상

Select(String, IXmlNamespaceResolver)

네임스페이스 접두사를 확인하기 위해 지정된 IXmlNamespaceResolver 개체와 함께 XPath 식을 사용하여 노드 집합을 선택합니다.

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ Select(System::String ^ xpath, System::Xml::IXmlNamespaceResolver ^ resolver);
public virtual System.Xml.XPath.XPathNodeIterator Select (string xpath, System.Xml.IXmlNamespaceResolver? resolver);
public virtual System.Xml.XPath.XPathNodeIterator Select (string xpath, System.Xml.IXmlNamespaceResolver resolver);
abstract member Select : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNodeIterator
override this.Select : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function Select (xpath As String, resolver As IXmlNamespaceResolver) As XPathNodeIterator

매개 변수

xpath
String

XPath 식을 나타내는 String입니다.

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver 개체는 네임스페이스 접두사를 확인하는 데 사용됩니다.

반환

XPathNodeIterator

선택된 노드 집합을 가리키는 XPathNodeIterator입니다.

예외

XPath 식에 오류가 포함되어 있거나 해당 반환 형식이 노드 집합이 아닙니다.

XPath 식이 잘못되었습니다.

예제

다음 예제에서는 XPath 식에서 네임스페이 Select 스 접두사 확인에 지정된 개체가 XmlNamespaceManager 있는 메서드를 사용하여 노드 집합을 선택하는 방법을 보여 줍니다.

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

XmlNamespaceManager^ manager = gcnew XmlNamespaceManager(navigator->NameTable);
manager->AddNamespace("bk", "http://www.contoso.com/books");

XPathNodeIterator^ nodes = navigator->Select("/bk:bookstore/bk:book/bk:price", manager);
// Move to the first node bk:price node.
if(nodes->MoveNext())
{
    // Now nodes.Current points to the first selected node.
XPathNavigator^ nodesNavigator = nodes->Current;

    // Select all the descendants of the current price node.
XPathNodeIterator^ nodesText = nodesNavigator->SelectDescendants(XPathNodeType::Text, false);

    while(nodesText->MoveNext())
    {
Console::WriteLine(nodesText->Current->Value);
    }
}
XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

XPathNodeIterator nodes = navigator.Select("/bk:bookstore/bk:book/bk:price", manager);
// Move to the first node bk:price node
if(nodes.MoveNext())
{
    // now nodes.Current points to the first selected node
    XPathNavigator nodesNavigator = nodes.Current;

    //select all the descendants of the current price node
    XPathNodeIterator nodesText =
       nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

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

Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
manager.AddNamespace("bk", "http://www.contoso.com/books")

Dim nodes As XPathNodeIterator = navigator.Select("/bk:bookstore/bk:book/bk:price", manager)
' Move to the first node bk:price node.
If (nodes.MoveNext()) Then
    ' Now nodes.Current points to the first selected node.
    Dim nodesNavigator As XPathNavigator = nodes.Current

    ' Select all the descendants of the current price node.
    Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

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

이 예제에서는 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>  

적용 대상