XmlNode.SelectSingleNode 方法

定义

选择匹配 XPath 表达式的第一个 XmlNode

重载

SelectSingleNode(String)

选择匹配 XPath 表达式的第一个 XmlNode

SelectSingleNode(String, XmlNamespaceManager)

选择匹配 XPath 表达式的第一个 XmlNode。 XPath 表达式中的任何前缀都使用提供的 XmlNamespaceManager 进行解析。

示例

以下示例返回具有匹配作者名称的第一本书。 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)

选择匹配 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 示例

返回

XmlNode

与 XPath 查询匹配的第一个 XmlNode;如果未找到任何匹配节点,则为 null

例外

XPath 表达式包含前缀。

示例

以下示例更改了第一本简·奥斯滕书的价格。

#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 表达式需要命名空间解析,则必须使用 SelectSingleNode 采用参数作为其参数的 XmlNamespaceManager 重载。 用于 XmlNamespaceManager 解析命名空间。

备注

如果 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'""]")  

此方法是文档对象模型的 Microsoft 扩展, (DOM) 。

另请参阅

适用于

SelectSingleNode(String, XmlNamespaceManager)

选择匹配 XPath 表达式的第一个 XmlNode。 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 表达式。 请参见 XPath 示例

nsmgr
XmlNamespaceManager

一个 XmlNamespaceManager,用于为 XPath 表达式中的前缀解析命名空间。

返回

XmlNode

与 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)  

此方法是文档对象模型的 Microsoft 扩展, (DOM) 。

另请参阅

适用于