次の方法で共有


XmlNode.SelectSingleNode メソッド

XPath 式と一致する最初の XmlNode を選択します。

オーバーロードの一覧

XPath 式と一致する最初の XmlNode を選択します。

[Visual Basic] Overloads Public Function SelectSingleNode(String) As XmlNode

[C#] public XmlNode SelectSingleNode(string);

[C++] public: XmlNode* SelectSingleNode(String*);

[JScript] public function SelectSingleNode(String) : XmlNode;

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

[Visual Basic] Overloads Public Function SelectSingleNode(String, XmlNamespaceManager) As XmlNode

[C#] public XmlNode SelectSingleNode(string, XmlNamespaceManager);

[C++] public: XmlNode* SelectSingleNode(String*, XmlNamespaceManager*);

[JScript] public function SelectSingleNode(String, XmlNamespaceManager) : XmlNode;

使用例

[Visual Basic, C#, C++] 一致する ISBN 値を持つ本を選択する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、SelectSingleNode のオーバーロード形式のうちの 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 the book node with the matching attribute value.
      Dim book as XmlNode 
      Dim root as XmlElement = doc.DocumentElement
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr)

      Console.WriteLine(book.OuterXml)

  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 the book node with the matching attribute value.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);

      Console.WriteLine(book.OuterXml);

  }
}

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

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 the book node with the matching attribute value.
   XmlNode * book;
   XmlElement * root = doc -> DocumentElement;
   book = root -> SelectSingleNode(S"descendant::book->Item[@bk:ISBN='1-861001-57-6']", nsmgr);

   Console::WriteLine(book -> OuterXml);
}

この例では、入力として、 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 名前空間