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.
Create the XML Document
- 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>
- Save the document as Books.xml in the root folder of your hard disk.
Create the Visual Basic .NET Application
- In Visual Studio .NET, create a new Visual Basic .NET Windows Application project.
- Drag a CommandButton control from the Toolbox to the designer surface of Form1.vb.
- 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("c:\books.xml")
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("//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.
For Each MSPressBook In MSPressBookList
System.Diagnostics.Debug.WriteLine(MSPressBook.InnerText)
Next
System.Diagnostics.Debug.WriteLine(vbCrLf & "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 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("//Title[.='XML Step by Step']")
'Determine whether a matching node is located.
If Not bookNode Is Nothing Then
System.Diagnostics.Debug.WriteLine("Located title 'XML Step by Step'")
Else
System.Diagnostics.Debug.WriteLine("Could not locate title 'XML Step by Step'")
End If
Test the Code
- Save and run the Visual Basic .NET project.
- 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'
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
316913
(http://support.microsoft.com/kb/316913/EN-US/
)
HOW TO: Specify Namespaces When You Use an XmlDocument to Execute XPath Queries