XmlNode.SelectNodes 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
选择匹配 XPath 表达式的节点列表。
重载
| SelectNodes(String) | 选择匹配 XPath 表达式的节点列表。 | 
| SelectNodes(String, XmlNamespaceManager) | 选择匹配 XPath 表达式的节点列表。 XPath 表达式中的任何前缀都使用提供的 XmlNamespaceManager 进行解析。 | 
注解
XPath 表达式可以包含命名空间。 使用 XmlNamespaceManager 支持命名空间解析。 如果 XPath 表达式包含前缀,则必须将前缀和命名空间 URI 对添加到 。XmlNamespaceManager
注意
如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。 如果 XML 包含默认命名空间,则仍必须将前缀和命名空间 URI 添加到 ; XmlNamespaceManager否则,将不会选择任何节点。
有关详细信息,请参阅 使用 XPath 导航选择节点。 对于代码示例,请从上一部分中的重载列表中选择一个重载。
SelectNodes(String)
- Source:
- XmlNode.cs
- Source:
- XmlNode.cs
- Source:
- XmlNode.cs
选择匹配 XPath 表达式的节点列表。
public:
 System::Xml::XmlNodeList ^ SelectNodes(System::String ^ xpath);public System.Xml.XmlNodeList? SelectNodes (string xpath);public System.Xml.XmlNodeList SelectNodes (string xpath);member this.SelectNodes : string -> System.Xml.XmlNodeListPublic Function SelectNodes (xpath As String) As XmlNodeList参数
- xpath
- String
XPath 表达式。
返回
一个 XmlNodeList,包含匹配 XPath 查询的节点集合。
例外
XPath 表达式包含前缀。 请参见 XPath 示例。
示例
以下示例选择作者姓氏为 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" );
   XmlNodeList^ nodeList;
   XmlNode^ root = doc->DocumentElement;
   nodeList = root->SelectNodes( "descendant::book[author/last-name='Austen']" );
   
   //Change the price on the books.
   System::Collections::IEnumerator^ myEnum = nodeList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      XmlNode^ book = safe_cast<XmlNode^>(myEnum->Current);
      book->LastChild->InnerText = "15.95";
   }
   Console::WriteLine( "Display the modified XML document...." );
   doc->Save( Console::Out );
}
using System;
using System.Xml;
public class Sample6
{
    public static void Main()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("booksort.xml");
        XmlNodeList nodeList;
        XmlNode root = doc.DocumentElement;
        nodeList = root.SelectNodes("descendant::book[author/last-name='Austen']");
        //Change the price on the books.
        foreach (XmlNode book in nodeList)
        {
            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 nodeList as XmlNodeList 
    Dim root as XmlNode = doc.DocumentElement
    nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']")
 
    'Change the price on the books.
    for each book in nodeList      
      book.LastChild.InnerText="15.95"
    next 
    Console.WriteLine("Display the modified XML document....")
    doc.Save(Console.Out)
    
  end sub
end class
此示例使用以下 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 表达式需要命名空间解析,则必须使用 SelectNodes 采用 XmlNamespaceManager 作为其参数的重载。 
              XmlNamespaceManager用于解析命名空间。
注意
如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。 如果 XML 包含默认命名空间,则仍 XmlNamespaceManager 必须使用 并向其添加前缀和命名空间 URI;否则,将不会选择任何节点。 有关详细信息,请参阅 使用 XPath 导航选择节点。
注意
在表述 XPath 表达式时,一个常见问题是如何在表达式中包含单引号 (“) 或双引号 (”) 。 如果必须搜索包含单引号的值,则必须将字符串括在双引号中。 如果需要搜索包含双引号的值,则必须将字符串括在单引号中。
例如,假设你具有以下 XML:
<bookstore>
  <book>
    <title>'Emma'</title>
  </book>
</bookstore>
以下 Visual Basic 代码选择包含单引号的元素:
nodeList = root.SelectNodes("//book[contains(title,""'Emma'"")]")
此方法是文档对象模型的 Microsoft 扩展, (DOM) 。
XmlNodeList此方法返回的对象将有效,而基础文档保持不变。 如果基础文档发生更改,可能会返回意外结果 () 不会引发异常。
另请参阅
适用于
SelectNodes(String, XmlNamespaceManager)
- Source:
- XmlNode.cs
- Source:
- XmlNode.cs
- Source:
- XmlNode.cs
选择匹配 XPath 表达式的节点列表。 XPath 表达式中的任何前缀都使用提供的 XmlNamespaceManager 进行解析。
public:
 System::Xml::XmlNodeList ^ SelectNodes(System::String ^ xpath, System::Xml::XmlNamespaceManager ^ nsmgr);public System.Xml.XmlNodeList? SelectNodes (string xpath, System.Xml.XmlNamespaceManager nsmgr);public System.Xml.XmlNodeList SelectNodes (string xpath, System.Xml.XmlNamespaceManager nsmgr);member this.SelectNodes : string * System.Xml.XmlNamespaceManager -> System.Xml.XmlNodeListPublic Function SelectNodes (xpath As String, nsmgr As XmlNamespaceManager) As XmlNodeList参数
- nsmgr
- XmlNamespaceManager
一个 XmlNamespaceManager,用于为 XPath 表达式中的前缀解析命名空间。
返回
一个 XmlNodeList,包含匹配 XPath 查询的节点集合。
例外
XPath 表达式包含 XmlNamespaceManager 中没有定义的前缀。
示例
以下示例显示每个 ISBN 属性的值。 此示例使用 XmlElement 从 XmlNode 类继承的 对象。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Collections;
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 and display the value of all the ISBN attributes.
   XmlNodeList^ nodeList;
   XmlElement^ root = doc->DocumentElement;
   nodeList = root->SelectNodes( "/bookstore/book/@bk:ISBN", nsmgr );
   IEnumerator^ myEnum = nodeList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      XmlNode^ isbn = safe_cast<XmlNode^>(myEnum->Current);
      Console::WriteLine( isbn->Value );
   }
}
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 and display the value of all the ISBN attributes.
      XmlNodeList nodeList;
      XmlElement root = doc.DocumentElement;
      nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
      foreach (XmlNode isbn in nodeList){
        Console.WriteLine(isbn.Value);
      }
   }
}
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 and display the value of all the ISBN attributes.
      Dim nodeList as XmlNodeList 
      Dim root as XmlElement = doc.DocumentElement
      nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr)
      Dim isbn as XmlNode
      for each isbn in nodeList
        Console.WriteLine(isbn.Value)
      next
  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");
XmlNodeList nodelist = doc.SelectNodes("//ab:book", nsmgr);
注意
在表述 XPath 表达式时,一个常见问题是如何在表达式中包含单引号 (“) 或双引号 (”) 。 如果必须搜索包含单引号的值,则必须将字符串括在双引号中。 如果需要搜索包含双引号的值,则必须将字符串括在单引号中。
例如,假设你具有以下 XML:
<bookstore xmlns="http://www.lucernepublishing.com">
  <book>
    <title>'Emma'</title>
  </book>
</bookstore>
以下 Visual Basic 代码选择包含单引号的元素:
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com")
nodeList = root.SelectNodes("//ab:book[contains(ab:title,""'Emma'"")]", nsmgr)
此方法是文档对象模型的 Microsoft 扩展, (DOM) 。
XmlNodeList此方法返回的对象将有效,而基础文档保持不变。 如果基础文档发生更改,可能会返回意外结果 () 不会引发异常。