Microsoft KB Archive/317018

= HOW TO: Use the System.Xml.XmlDocument Class to Run XPath Queries in Visual Basic .NET =

Article ID: 317018

Article Last Modified on 9/22/2003

-

APPLIES TO


 * 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
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q317018



For a Microsoft Visual C# .NET version of this article, see 318499.



IN THIS TASK
SUMMARY
 * Create the XML Document
 * Create the Visual Basic .NET Application
 * Test the Code

REFERENCES



SUMMARY
This step-by-step article describes how to run 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 run XPath queries against XML data that is loaded into the DOM.

back to the top

Create the 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</Title> 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 Visual Basic .NET Application
<ol> <li>In Visual Studio .NET, create a new Visual Basic .NET Windows Application project.</li> <li>Drag a CommandButton control from the Toolbox to the designer surface of Form1.vb.</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 particular attention to the comments that explain the usage of the SelectNodes and the SelectSingleNode methods of the XmlDocument class. 'Instantiate an XmlDocument object. Dim xmldoc As New System.Xml.XmlDocument

'Load Books.xml into the DOM. xmldoc.Load(&quot;c:\books.xml&quot;)

Dim MSPressBookList As System.Xml.XmlNodeList Dim MSPressBook As System.Xml.XmlNode

'Run 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.

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. For Each MSPressBook In MSPressBookList System.Diagnostics.Debug.WriteLine(MSPressBook.InnerText) Next

System.Diagnostics.Debug.WriteLine(vbCrLf & &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 run.

'Only the first matching node is returned when multiple matching nodes exist 'for the specified XPath query expression.

Dim bookNode As System.Xml.XmlNode = xmldoc.SelectSingleNode(&quot;//Title[.='XML Step by Step']&quot;)

'Determine whether a matching node is located. If Not bookNode Is Nothing Then 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;) End If                   </li></ol>

back to the top

Test the Code
<ol> <li>Save and run the Visual Basic .NET project.</li> <li> When the form is displayed, click the command button to run 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">