HOW TO: Use the System.Xml.XmlDocument Class to Execute XPath Queries in Visual C# .NET

This article was previously published under Q318499
This article has been archived. It is offered "as is" and will no longer be updated.
This step-by-step article describes how to execute XPath queries by using the SelectNodes and SelectSingleNode methods of the XmlDocument class.

The System.Xml.XmlDocument class implements the core XML Document Object Model (DOM) parser for the .NET Framework. You can use the SelectNodes and the SelectSingleNode methods of the System.Xml.XmlDocument class to programmatically execute XPath queries against XML data that is loaded into the DOM.

back to the top

Create the Sample XML Document

  1. Use Notepad or a similar text editor to create a new XML document that contains the following code:
    <?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. Save the document as Books.xml in the root folder of your hard disk.
back to the top

Create the Sample Visual C# .NET Application

  1. In Visual Studio .NET, create a new Visual C# .NET Windows Application project.
  2. Drag a CommandButton control from the Toolbox onto the designer surface of Form1.cs.
  3. Paste the following code in the Click event procedure of the command button:NOTE: Read the inline comments to understand the functionality of the code. Pay specific attention to the comments that explain the usage of the SelectNodes and the SelectSingleNode methods of the XmlDocument class.
    //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'");}
back to the top

Test the Sample Code

  1. Save and run the project.
  2. When the form is displayed, click the command button to execute the code that is described in the previous section. The results of the XPath queries are displayed in the Visual Studio .NET Output window, as follows:
    Books published by MSPress...**************************...XML Step by StepDeveloping XML solutionsLooking for the title 'XML Step by Step'...***************************************...Located title 'XML Step by Step'					
back to the top
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
316913 HOW TO: Specify Namespaces When You Use an XmlDocument to Execute XPath Queries in Visual Basic .NET
313828 INFO: Roadmap for Executing XPath Queries in .NET Applications
back to the top

Article ID: 318499 - Last Review: 12/07/2015 09:03:24 - Revision: 4.4

Microsoft .NET Framework Class Libraries 1.0, Microsoft .NET Framework Class Libraries 1.1, Microsoft Visual Studio .NET 2002 Professional Edition, Microsoft Visual Studio .NET 2003 Professional Edition, Microsoft Visual Studio .NET 2002 Enterprise Developer, Microsoft Visual Studio .NET 2003 Enterprise Developer

  • kbnosurvey kbarchive kbhowtomaster KB318499