Microsoft KB Archive/318499

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

Article ID: 318499

Article Last Modified on 1/23/2004

-

APPLIES TO


 * 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

-



This article was previously published under Q318499



For a Microsoft Visual Basic .NET version of this article, see 317018.

IN THIS TASK
SUMMARY
 * Create the Sample XML Document
 * Create the Sample Visual C# .NET Application
 * Test the Sample Code

REFERENCES



SUMMARY
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
  Use Notepad or a similar text editor to create a new XML document that contains the following code:    Beginning XML Wrox   XML Step by Step MSPress</Publisher> </Book>  Professional XML</Title> Wrox</Publisher> </Book>  Developing XML solutions</Title> MSPress</Publisher> </Book> </Books> </li> <li>Save the document as Books.xml in the root folder of your hard disk.</li></ol>

back to the top

Create the Sample Visual C# .NET Application
<ol> <li>In Visual Studio .NET, create a new Visual C# .NET Windows Application project.</li> <li>Drag a CommandButton control from the Toolbox onto the designer surface of Form1.cs.</li> <li> 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(&quot;C:\\books.xml&quot;);

//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(&quot;//Publisher[. = 'MSPress']/parent::node/Title&quot;);

System.Diagnostics.Debug.WriteLine(&quot;Books published by MSPress...&quot;); System.Diagnostics.Debug.WriteLine(&quot;**************************...&quot;);

//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( &quot;Looking for the title 'XML Step by Step'...&quot;); System.Diagnostics.Debug.WriteLine(&quot;***************************************...&quot;);

//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(&quot;//Title[.='XML Step by Step']&quot;);

//Determine whether a matching node is located. if (!(bookNode == null)) {   System.Diagnostics.Debug.WriteLine(&quot;Located title 'XML Step by Step'&quot;); } else {           System.Diagnostics.Debug.WriteLine(&quot;Could not locate title 'XML Step by Step'&quot;);

} </li></ol>

back to the top

Test the Sample Code
<ol> <li>Save and run the project.</li> <li> 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 Step Developing XML solutions

Looking for the title 'XML Step by Step'... Located title 'XML Step by Step' </li></ol>

back to the top

<div class="references_section">