本分步指南介绍了如何通过使用 SelectNodes 和 SelectSingleNode如下 类的方法中执行 XPath 查询。
System.Xml.XmlDocument 类实现为.net 框架的核心 XML 文档对象模型 (DOM) 分析器。可以以编程方式执行对 XML 数据加载到该 DOM.的 XPath 查询使用SelectNodes 和SelectSingleNodeSystem.Xml.XmlDocument 类的方法
System.Xml.XmlDocument 类实现为.net 框架的核心 XML 文档对象模型 (DOM) 分析器。可以以编程方式执行对 XML 数据加载到该 DOM.的 XPath 查询使用SelectNodes 和SelectSingleNodeSystem.Xml.XmlDocument 类的方法
创建示例 XML 文档
- 使用记事本或类似的文本编辑器创建一个新的 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>
- 将文档另存为 Books.xml 在您的硬盘的根文件夹中。
创建示例 Visual C# 应用.net 程序
- 在 Visual Studio.net 中,创建一个新的 Visual C#.net Windows 应用程序项目。
- 将 命令按钮 控件从工具箱拖到 Form1.cs 的设计器图面上。
- 将以下代码粘贴到在命令按钮的 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'"); }
测试示例代码
- 保存并运行该项目。
- 在显示窗体时单击命令按钮执行上一节中描述的代码。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'