使用 System.Xml.XmlDocument 类可在 Visual C#.net 中执行 XPath 查询

 本分步指南介绍了如何通过使用 SelectNodesSelectSingleNode如下 类的方法中执行 XPath 查询。

System.Xml.XmlDocument 类实现为.net 框架的核心 XML 文档对象模型 (DOM) 分析器。可以以编程方式执行对 XML 数据加载到该 DOM.的 XPath 查询使用SelectNodesSelectSingleNodeSystem.Xml.XmlDocument 类的方法

创建示例 XML 文档

  1. 使用记事本或类似的文本编辑器创建一个新的 XML 文档,其中包含下面的代码:
    <?xml version='1.0'?>
    <Books>
    <Book>
      <Title>Beginning XML</Title>
      <Publisher>Wrox</Publisher>
    </Book>
    <Book>
      <Title>XML Step by Step</Title>
      <Publisher>MSPress</Publisher>
    </Book>
    <Book>
      <Title>Professional XML</Title>
      <Publisher>Wrox</Publisher>
    </Book>
    <Book>
      <Title>Developing XML solutions</Title>
      <Publisher>MSPress</Publisher>
    </Book>
    </Books>
    

  2. 将文档另存为 Books.xml 在您的硬盘的根文件夹中。

创建示例 Visual C# 应用.net 程序

  1. 在 Visual Studio.net 中,创建一个新的 Visual C#.net Windows 应用程序项目。
  2. 命令按钮 控件从工具箱拖到 Form1.cs 的设计器图面上。
  3. 将以下代码粘贴到在命令按钮的 Click 事件过程: : 读取内嵌批注,以了解代码的功能。请注意特定的解释SelectNodes 的使用情况和SelectSingleNode 方法 如下 类的评论。
    //Instantiate an XmlDocument object.
    System.Xml.XmlDocument  xmldoc = new System.Xml.XmlDocument();
    
    //Load Books.xml into the DOM.
    xmldoc.Load("C:\\books.xml");
    
    //Execute the SelectNodes method to identify titles that are published by MSPress.
    //The SelectNodes method returns an XmlNodeList object.
    //The XmlNodeList is a collection of XmlNode objects.
    //The XmlNodeList that is returned by SelectNodes contains one XmlNode for each node that the XPath query selects.
    
    System.Xml.XmlNodeList MSPressBookList = xmldoc.SelectNodes("//Publisher[. = 'MSPress']/parent::node()/Title");
    
    System.Diagnostics.Debug.WriteLine("Books published by MSPress...");
    			System.Diagnostics.Debug.WriteLine("**************************...");
    
    //Use an XmlNode object to iterate through the XmlNodeList that SelectNodes returns. 
    foreach (System.Xml.XmlNode MSPressBook in MSPressBookList)
    {
    				     System.Diagnostics.Debug.WriteLine(MSPressBook.InnerText);
    }
    
    System.Diagnostics.Debug.WriteLine( "Looking for the title 'XML Step by Step'...");
    			System.Diagnostics.Debug.WriteLine("***************************************...");
    
    
    //Use the SelectSingleNode method to locate a specific title.
    
    //The SelectSingleNode method returns a single XmlNode object.
    
    //The SelectSingleNode method is typically used to specify an XPath query expression 
    //that results in a single matching node when it is executed.
    
    //Only the first matching node is returned when multiple matching nodes exist
    //for the specified XPath query expression. 
    
    System.Xml.XmlNode bookNode = xmldoc.SelectSingleNode("//Title[.='XML Step by Step']");
    
    //Determine whether a matching node is located. 
    if (!(bookNode == null)) 
    {
    	System.Diagnostics.Debug.WriteLine("Located title 'XML Step by Step'");
    }
    else
    {    
            System.Diagnostics.Debug.WriteLine("Could not locate title 'XML Step by Step'");
    
    }

测试示例代码

  1. 保存并运行该项目。
  2. 在显示窗体时单击命令按钮执行上一节中描述的代码。XPath 查询的结果显示在 Visual Studio.net 输出窗口中,如下所示:
    Books published by MSPress...
    **************************...
    XML Step by Step
    Developing XML solutions
    
    Looking for the title 'XML Step by Step'...
    ***************************************...
    Located title 'XML Step by Step'
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值