XmlNode.SelectSingleNode 方法

定義

選取符合 XPath 運算式的第一個 XmlNode

多載

SelectSingleNode(String)

選取符合 XPath 運算式的第一個 XmlNode

SelectSingleNode(String, XmlNamespaceManager)

選取符合 XPath 運算式的第一個 XmlNode。 使用提供的 XmlNamespaceManager 解析 XPath 運算式中找到的任何前置詞。

範例

下列範例會傳回具有相符作者名稱的第一本書籍。 XmlNamespaceManager 會解析 XPath 運算式中的預設命名空間。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( L"newbooks.xml" );
   
   // Create an XmlNamespaceManager to resolve the default namespace.
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( doc->NameTable );
   nsmgr->AddNamespace( L"bk", L"urn:newbooks-schema" );
   
   // Select the first book written by an author whose last name is Atwood.
   XmlNode^ book;
   XmlElement^ root = doc->DocumentElement;
   book = root->SelectSingleNode( L"descendant::bk:book[bk:author/bk:last-name='Atwood']", nsmgr );
   Console::WriteLine( book->OuterXml );
   return 0;
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

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

      // Create an XmlNamespaceManager to resolve the default namespace.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk", "urn:newbooks-schema");

      // Select the first book written by an author whose last name is Atwood.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
     book = root.SelectSingleNode("descendant::bk:book[bk:author/bk:last-name='Atwood']", nsmgr);

      Console.WriteLine(book.OuterXml);
  }
}
Imports System.IO
Imports System.Xml

Public Class Sample

  Public Shared Sub Main()

      Dim doc As XmlDocument = New XmlDocument()
      doc.Load("newbooks.xml")

      'Create an XmlNamespaceManager for resolving namespaces.
      Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
      nsmgr.AddNamespace("bk", "urn:newbooks-schema")

      'Select the book written by an author whose last name is Atwood.
      Dim book As XmlNode 
      Dim root As XmlElement = doc.DocumentElement
      book = root.SelectSingleNode("descendant::bk:book[bk:author/bk:last-name='Atwood']", nsmgr)

      Console.WriteLine(book.OuterXml)

  End Sub
End Class

此範例會使用 檔案 newbooks.xml ,作為輸入。

<?xml version='1.0'?>
<bookstore xmlns="urn:newbooks-schema">
  <book genre="novel" style="hardcover">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" style="other">
    <title>The Poisonwood Bible</title>
    <author>
      <first-name>Barbara</first-name>
      <last-name>Kingsolver</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>

備註

XPath 運算式可包含命名空間。 命名空間解析可透過 XmlNamespaceManager 予以支援。 如果 XPath 運算式包含前置詞,則必須將前置詞和命名空間 URI 配對新增至 XmlNamespaceManager

注意

如果 XPath 運算式不包含前置詞,則會假設命名空間 URI 是空的命名空間。 如果您的 XML 包含預設命名空間,您仍然必須將前置詞和命名空間 URI 新增至 XmlNamespaceManager ,否則不會選取任何節點。 如需詳細資訊,請參閱 使用 XPath 流覽選取節點

SelectSingleNode(String)

Source:
XmlNode.cs
Source:
XmlNode.cs
Source:
XmlNode.cs

選取符合 XPath 運算式的第一個 XmlNode

public:
 System::Xml::XmlNode ^ SelectSingleNode(System::String ^ xpath);
public System.Xml.XmlNode? SelectSingleNode (string xpath);
public System.Xml.XmlNode SelectSingleNode (string xpath);
member this.SelectSingleNode : string -> System.Xml.XmlNode
Public Function SelectSingleNode (xpath As String) As XmlNode

參數

xpath
String

XPath 運算式。 請參閱 XPath 範例

傳回

符合 XPath 查詢的第一個 XmlNode,如果找不到符合的節點,則為 null

例外狀況

XPath 運算式包含前置詞。

範例

下列範例會變更第一張 Jane Austen 書籍的價格。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew 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 );
}
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);
  }
}
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

此範例會使用 檔案 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>

備註

如果 XPath 運算式需要命名空間解析,您必須使用採用 做為其引數的多 SelectSingleNodeXmlNamespaceManagerXmlNamespaceManager用來解析命名空間。

注意

如果 XPath 運算式不包含前置詞,則會假設命名空間 URI 是空的命名空間。 如果您的 XML 包含預設命名空間,您仍然 XmlNamespaceManager 必須使用 ,並在其中新增前置詞和命名空間 URI,否則不會取得選取的節點。 如需詳細資訊,請參閱 使用 XPath 流覽選取節點

注意

撰寫 XPath 運算式時的常見問題是如何在運算式中包含單引號 () 或雙引號 (「) 。 如果您必須搜尋包含單引號的值,則必須以雙引號括住字串。 如果您需要搜尋包含雙引號的值,則必須以單引號括住字串。

例如,假設您有下列 XML:

<bookstore>
  <book>
    <title>&apos;Emma&apos;</title>
  </book>
</bookstore>

下列 Visual Basic 程式碼會選取包含單引號的專案:

book = root.SelectSingleNode("descendant::book[title=""'Emma'""]")

這個方法是檔物件模型 (DOM) Microsoft延伸模組。

另請參閱

適用於

SelectSingleNode(String, XmlNamespaceManager)

Source:
XmlNode.cs
Source:
XmlNode.cs
Source:
XmlNode.cs

選取符合 XPath 運算式的第一個 XmlNode。 使用提供的 XmlNamespaceManager 解析 XPath 運算式中找到的任何前置詞。

public:
 System::Xml::XmlNode ^ SelectSingleNode(System::String ^ xpath, System::Xml::XmlNamespaceManager ^ nsmgr);
public System.Xml.XmlNode? SelectSingleNode (string xpath, System.Xml.XmlNamespaceManager nsmgr);
public System.Xml.XmlNode SelectSingleNode (string xpath, System.Xml.XmlNamespaceManager nsmgr);
member this.SelectSingleNode : string * System.Xml.XmlNamespaceManager -> System.Xml.XmlNode
Public Function SelectSingleNode (xpath As String, nsmgr As XmlNamespaceManager) As XmlNode

參數

xpath
String

XPath 運算式。 請參閱 XPath 範例

nsmgr
XmlNamespaceManager

XmlNamespaceManager,用來解析 XPath 運算式中前置詞的命名空間。

傳回

符合 XPath 查詢的第一個 XmlNode,如果找不到符合的節點,則為 null

例外狀況

XPath 運算式包含未定義於 XmlNamespaceManager 中的前置詞。

範例

下列範例會選取具有相符 ISBN 值的書籍。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "booksort.xml" );
   
   //Create an XmlNamespaceManager for resolving namespaces.
   XmlNamespaceManager^ nsmgr = gcnew 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->Item[@bk:ISBN='1-861001-57-6']", nsmgr );
   Console::WriteLine( book->OuterXml );
}
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);
  }
}
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

此範例會使用 檔案 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>

備註

XPath 運算式可包含命名空間。 命名空間解析可透過 XmlNamespaceManager 予以支援。 如果 XPath 運算式包含前置詞,則必須將前置詞和命名空間 URI 配對新增至 XmlNamespaceManager

注意

如果 XPath 運算式不包含前置詞,則會假設命名空間 URI 是空的命名空間。 如果您的 XML 包含預設命名空間,您仍然必須將前置詞和命名空間 URI 新增至 XmlNamespaceManager ,否則不會選取節點。 如需詳細資訊,請參閱 使用 XPath 流覽選取節點

例如,如果您有下列 XML:

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

下列 C# 程式碼會選取第一個書籍節點:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);

注意

撰寫 XPath 運算式時的常見問題是如何在運算式中包含單引號 () 或雙引號 (「) 。 如果您必須搜尋包含單引號的值,則必須以雙引號括住字串。 如果您需要搜尋包含雙引號的值,則必須以單引號括住字串。

例如,假設您有下列 XML:

<bookstore xmlns="http://www.lucernepublishing.com">
  <book>
    <title>&apos;Emma&apos;</title>
  </book>
</bookstore>

下列 Visual Basic 程式碼會選取包含單引號的專案:

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com")
book = root.SelectSingleNode("descendant::ab:book[ab:title=""'Emma'""]", nsmgr)

這個方法是檔物件模型 (DOM) Microsoft延伸模組。

另請參閱

適用於