XmlNode.SelectNodes 方法 (String)

选择匹配 XPath 表达式的节点列表。

[VisualBasic]
Overloads Public Function SelectNodes( _
   ByVal xpath As String _
) As XmlNodeList
[C#]
public XmlNodeList SelectNodes(
   string xpath
);
[C++]
public: XmlNodeList* SelectNodes(
   String* xpath
);
[JScript]
public function SelectNodes(
   xpath : String
) : XmlNodeList;
参数
xpath
XPath 表达式。
返回值

一个 XmlNodeList,包含匹配 XPath 查询的节点集合。

异常
异常类型条件
XPathExceptionXPath 表达式包含前缀。
备注

如果 XPath 表达式需要命名空间解析,必须使用接受 XmlNamespaceManager 作为参数的 SelectNodes 重载。XmlNamespaceManager 用于解析命名空间。

注意如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。如果 XML 包含默认命名空间,则您仍必须使用 XmlNamespaceManager 并向其添加前缀和命名空间 URI,否则将得不到任何选定的节点。

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

示例

[VisualBasic,C#,C++] 下面的示例更改所有 Jane Austen 写的书的价格。

[VisualBasic] 
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 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

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample {

  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);
    
  }
}

[C++] 
#using 
 
 
#using 
 
 
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    XmlDocument* doc = new XmlDocument();
    doc->Load(S"booksort.xml");

    XmlNodeList* nodeList;
    XmlNode* root = doc->DocumentElement;

    nodeList=root->SelectNodes(S"descendant::book[author/last-name='Austen']");
 
    //Change the price on the books.
    System::Collections::IEnumerator* myEnum = nodeList->GetEnumerator();
    while (myEnum->MoveNext())
    {
        XmlNode* book = __try_cast(myEnum->Current);
        book->LastChild->InnerText=S"15.95";
    }

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

[VisualBasic,C#,C++] 该示例使用文件 booksort.xml 作为输入。

 
 

 
 

 
 
  
 
 
    
    
 
 
      
 
 Jane
      
 
 Austen
    
    
 
 24.95
  
  
 
 
    
    
 
 
      
 
 Margaret
      
 
 Atwood
    
    
 
 29.95
  
  
 
 
    
    
 
 
      
 
 Jane
      
 
 Austen
    
    
 
 19.95
  
  
 
 
    
    
 
 
      
 
 Jane
      
 
 Austen
    
    
 
 19.95
  

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值