次の方法で共有


XmlNode.SelectSingleNode メソッド (String)

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

Overloads Public Function SelectSingleNode( _
   ByVal xpath As String _) As XmlNode
[C#]
public XmlNode SelectSingleNode(stringxpath);
[C++]
public: XmlNode* SelectSingleNode(String* xpath);
[JScript]
public function SelectSingleNode(
   xpath : String) : XmlNode;

パラメータ

  • xpath
    XPath 式。

戻り値

XPath クエリと一致する最初の XmlNode 。一致するノードが見つからない場合は null 参照 (Visual Basic では Nothing) 。

例外

例外の種類 条件
XPathException XPath 式にプリフィックスが含まれています。

解説

XPath 式で名前空間の解決が必要な場合は、 XmlNamespaceManager を引数として取る SelectSingleNode オーバーロードを使用する必要があります。 XmlNamespaceManager は、名前空間を解決するために使用します。

メモ   XPath 式にプリフィックスが含まれていない場合、名前空間 URI は、空の名前空間であると見なされます。XML に既定の名前空間が含まれている場合でも、 XmlNamespaceManager を使用して、プリフィックスと名前空間 URI を追加する必要があります。そうしないと、選択したノードを取得できません。

このメソッドは、ドキュメント オブジェクト モデル (DOM: Document Object Model) に対する Microsoft 拡張機能です。

使用例

[Visual Basic, C#, C++] Jane Austen の最初の本の価格を変更する例を次に示します。

 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    'Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.Load("booksort.xml")
           
    Dim book as XmlNode
    Dim root as XmlNode = doc.DocumentElement

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']")
 
    'Change the price on the book.
    book.LastChild.InnerText="15.95"

    Console.WriteLine("Display the modified XML document....")
    doc.Save(Console.Out)
    
  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");

    XmlNode book;
    XmlNode root = doc.DocumentElement;

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']");
 
    //Change the price on the book.
    book.LastChild.InnerText="15.95";

    Console.WriteLine("Display the modified XML document....");
    doc.Save(Console.Out);    
  }
}

[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");

    XmlNode* book;
    XmlNode* root = doc->DocumentElement;

    book=root->SelectSingleNode(S"descendant::book[author/last-name='Austen']");
 
    //Change the price on the book.
    book->LastChild->InnerText=S"15.95";

    Console::WriteLine(S"Display the modified XML document....");
    doc->Save(Console::Out);    
}

この例では、入力として、 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XmlNode クラス | XmlNode メンバ | System.Xml 名前空間 | XmlNode.SelectSingleNode オーバーロードの一覧 | SelectNodes