次の方法で共有


XmlNode.SelectNodes メソッド

XPath 式と一致するノードのリストを選択します。

オーバーロードの一覧

XPath 式と一致するノードのリストを選択します。

[Visual Basic] Overloads Public Function SelectNodes(String) As XmlNodeList

[C#] public XmlNodeList SelectNodes(string);

[C++] public: XmlNodeList* SelectNodes(String*);

[JScript] public function SelectNodes(String) : XmlNodeList;

XPath 式と一致するノードのリストを選択します。XPath 式で見つかったプリフィックスは、指定した XmlNamespaceManager を使用して解決されます。

[Visual Basic] Overloads Public Function SelectNodes(String, XmlNamespaceManager) As XmlNodeList

[C#] public XmlNodeList SelectNodes(string, XmlNamespaceManager);

[C++] public: XmlNodeList* SelectNodes(String*, XmlNamespaceManager*);

[JScript] public function SelectNodes(String, XmlNamespaceManager) : XmlNodeList;

使用例

[Visual Basic, C#, C++] 各 ISBN 属性の値を表示する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、SelectNodes のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

      Dim doc as XmlDocument = new XmlDocument()
      doc.Load("booksort.xml")

      'Create an XmlNamespaceManager for resolving namespaces.
      Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable)
      nsmgr.AddNamespace("bk", "urn:samples")

      'Select and display the value of all the ISBN attributes.
      Dim nodeList as XmlNodeList 
      Dim root as XmlElement = doc.DocumentElement
      nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr)
      Dim isbn as XmlNode
      for each isbn in nodeList
        Console.WriteLine(isbn.Value)
      next

  end sub
end class

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

      XmlDocument doc = new XmlDocument();
      doc.Load("booksort.xml");

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk", "urn:samples");

      //Select and display the value of all the ISBN attributes.
      XmlNodeList nodeList;
      XmlElement root = doc.DocumentElement;
      nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
      foreach (XmlNode isbn in nodeList){
        Console.WriteLine(isbn.Value);
      }

   }

}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Collections;

int main() 
{
   XmlDocument* doc = new XmlDocument();
   doc -> Load(S"booksort.xml");

   // Create an XmlNamespaceManager for resolving namespaces.
   XmlNamespaceManager* nsmgr = new XmlNamespaceManager(doc -> NameTable);
   nsmgr -> AddNamespace(S"bk", S"urn:samples");

   // Select and display the value of all the ISBN attributes.
   XmlNodeList * nodeList;
   XmlElement * root = doc -> DocumentElement;
   nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN", nsmgr);
   IEnumerator* myEnum = nodeList->GetEnumerator();
   while (myEnum->MoveNext())
   {
      XmlNode* isbn = __try_cast<XmlNode*>(myEnum->Current);
      Console::WriteLine(isbn -> Value);
   }
}

この例では、入力として、 booksort.xml というファイルを使用しています。

<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

XmlNode クラス | XmlNode メンバ | System.Xml 名前空間