Language

XPathNavigator.Select メソッド

定義

指定した XPath 式を使用して、ノード セットを選択します。

オーバーロード

名前 説明
Select(String)

指定した XPath 式を使用して、ノード セットを選択します。

Select(XPathExpression)

指定した XPathExpressionを使用してノード セットを選択します。

Select(String, IXmlNamespaceResolver)

名前空間プレフィックスを解決するために指定した IXmlNamespaceResolver オブジェクトで、指定した XPath 式を使用してノード セットを選択します。

Select(String)

ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs

指定した 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 。

例外

XPath 式にエラーが含まれているか、その戻り値の型がノード セットではありません。

XPath 式が無効です。

例

次の例では、 Select メソッドを使用してノード セットを選択します。

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 は、選択したノードのセットを表します。 MoveNext XPathNodeIteratorメソッドを使用して、選択したノード セットを反復処理します。

次の C# コードは、選択したノードのセットを反復処理します。

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

Selectメソッドを使用する際に考慮すべき重要な注意事項を次に示します。

  • 引き続き、 XPathNavigator オブジェクトのナビゲーション メソッドのいずれかを使用して、 XPathNavigator内を移動できます。 XPathNavigator ナビゲーション メソッドは、XPathNodeIteratorで選択したノードとは無関係です。

  • Select メソッドの今後の呼び出しでは、新しいXPathNodeIterator呼び出しに一致するノードの選択されたセットを指す新しいSelect オブジェクトが返されます。 2 つの XPathNodeIterator オブジェクトは互いに完全に独立しています。

  • XPath 式で名前空間の解決が必要な場合は、 Select オーバーロードを使用します。このオーバーロードは、引数として XPathExpression を受け取ります。

  • このメソッドは、 XPathNavigatorの状態には影響しません。

こちらもご覧ください

適用対象

Select(XPathExpression)

ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs

指定した 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 。

例外

XPath 式にエラーが含まれているか、その戻り値の型がノード セットではありません。

XPath 式が無効です。

例

次の例では、 Select メソッドを使用してノード セットを選択します。

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 は、選択したノードのセットを表します。 選択したノード セットを反復処理するには、MoveNextでXPathNodeIteratorを使用します。

次の C# コードは、選択したノードのセットを反復処理します。

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

Selectメソッドを使用する際に考慮すべき重要な注意事項を次に示します。

  • 引き続き、 XPathNavigator オブジェクトのナビゲーション メソッドのいずれかを使用して、 XPathNavigator内を移動できます。 XPathNavigator ナビゲーション メソッドは、XPathNodeIteratorで選択したノードとは無関係です。

  • Select メソッドの今後の呼び出しでは、新しいXPathNodeIterator呼び出しに一致するノードの選択されたセットを指す新しいSelect オブジェクトが返されます。 2 つの XPathNodeIterator オブジェクトは互いに完全に独立しています。

  • XPathExpressionで名前空間の解決が必要な場合は、プレフィックスと名前空間 URI のペアをXmlNamespaceManagerに追加し、名前空間解決に使用するSetContextを指定するには、XmlNamespaceManager メソッドを呼び出す必要があります。

たとえば、ドキュメントに次の 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);

Note

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)

ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs
ソース:
XPathNavigator.cs

名前空間プレフィックスを解決するために指定した 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 。

例外

XPath 式にエラーが含まれているか、その戻り値の型がノード セットではありません。

XPath 式が無効です。

例

次の例は、XPath 式の名前空間プレフィックスを解決するために指定されたSelect オブジェクトでXmlNamespaceManager メソッドを使用してノード セットを選択する方法を示しています。

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>

適用対象