XmlNode.SelectSingleNode 方法

定義

選擇第一個 XmlNode 與 XPath 表達式相符的字母。

多載

名稱 Description
SelectSingleNode(String)

選擇第一個 XmlNode 與 XPath 表達式相符的字母。

SelectSingleNode(String, XmlNamespaceManager)

選擇第一個 XmlNode 與 XPath 表達式相符的字母。 XPath 表達式中發現的任何前綴皆可透過提供的 XmlNamespaceManager解析 。

範例

以下範例回傳第一本與作者名稱相符的書。 它 XmlNamespaceManager 解析了 XPath 表達式中的預設命名空間。

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 包含預設命名空間,你仍然必須在 中 XmlNamespaceManager加入前綴和命名空間 URI;否則,你將無法被選取任何節點。 如需詳細資訊,請參閱 使用 XPath 瀏覽選取節點

SelectSingleNode(String)

來源:
XmlNode.cs
來源:
XmlNode.cs
來源:
XmlNode.cs
來源:
XmlNode.cs
來源:
XmlNode.cs

選擇第一個 XmlNode 與 XPath 表達式相符的字母。

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 的表達方式。

傳回

第一個 XmlNode 與 XPath 查詢相符的,或 null 是找不到匹配節點。

例外狀況

XPath 表達式包含一個前綴。

範例

以下範例改變了第一本珍·奧斯汀書籍的價格。

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 表達式需要命名空間解析,你必須使用 SelectSingleNode 以 為 XmlNamespaceManager 參數的超載。 用來 XmlNamespaceManager 解析命名空間。

備註

若 XPath 表達式未包含前綴,則假設命名空間 URI 為空命名空間。 如果你的 XML 包含預設命名空間,你仍必須使用 並 XmlNamespaceManager 新增前綴和命名空間 URI;否則你不會得到被選中的節點。 如需詳細資訊,請參閱 使用 XPath 瀏覽選取節點

備註

在表達式中,一個常見的問題是如何在表達式中加入單引號(')或雙引號(“)。 如果你需要搜尋包含單一引號的數值,必須用雙引號包住字串。 如果你需要搜尋包含雙引號的數值,必須用單引號包住字串。

例如,假設您有下列 XML:

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

以下 Visual Basic 程式碼選擇包含單引號的元素:

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

此方法是 Microsoft 對文件物件模型(DOM)的擴充。

另請參閱

適用於

SelectSingleNode(String, XmlNamespaceManager)

來源:
XmlNode.cs
來源:
XmlNode.cs
來源:
XmlNode.cs
來源:
XmlNode.cs
來源:
XmlNode.cs

選擇第一個 XmlNode 與 XPath 表達式相符的字母。 XPath 表達式中發現的任何前綴皆可透過提供的 XmlNamespaceManager解析 。

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 的表達方式。

nsmgr
XmlNamespaceManager

XmlNamespaceManager用於解析 XPath 表達式中前綴的命名空間。

傳回

第一個 XmlNode 與 XPath 查詢相符的,或 null 是找不到匹配節點。

例外狀況

XPath 表達式包含一個未在 XmlNamespaceManager.

範例

以下範例選擇與 ISBN 值相符的書籍。

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 包含預設命名空間,你仍然必須在 中 XmlNamespaceManager加入前綴和命名空間 URI;否則你不會被選中節點。 如需詳細資訊,請參閱 使用 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);

備註

在表達式中,一個常見的問題是如何在表達式中加入單引號(')或雙引號(“)。 如果你需要搜尋包含單一引號的數值,必須用雙引號包住字串。 如果你需要搜尋包含雙引號的數值,必須用單引號包住字串。

例如,假設您有下列 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)

此方法是 Microsoft 對文件物件模型(DOM)的擴充。

另請參閱

適用於