Microsoft KB Archive/308062

= How To Specify Fully Qualified Element Names in XPath Queries by Using Visual Basic .NET =

Article ID: 308062

Article Last Modified on 7/14/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q308062



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

IN THIS TASK
SUMMARY
 * Create the XML File
 * Create the Visual Basic .NET Project

REFERENCES



SUMMARY
When you use the XmlDocument object to load and parse an Extensible Markup Language (XML) document, it is common programming practice to identify elements with a specific element name. This article demonstrates how to specify fully qualified element names in the  format to select nodes in an XmlDocument.

back to the top

Create the XML File
 On the Windows Start menu, point to Run, type notepad.exe, and then click OK to open Notepad.  Copy and paste the following XML code into Notepad:    Just XML   Professional XML </bk:Book>  XML Step by Step</bk:Title> </bk:Book>  XML By Example</bk:Title> </bk:Book> </bk:Books> </li> On the File menu, click Save.</li> In the Save As dialog box, in the Save as type text box, click All Files. In the File name text box, type Books.xml, and then click OK.</li></ol>

back to the top

Create the Visual Basic .NET Project
The following code sample uses the following objects and classes:
 * The XPathNavigator class: XPathNavigator is based on the XML Path Language (XPath) data model and provides the methods that are required to implement XPath queries over any data store.
 * The XPathExpression class: This class encapsulates a compiled XPath expression and is returned when you call Compile. The Select, Evaluate, and Matches methods use this class.
 * The XmlNamespaceManager class: XmlNamespaceManager resolves, adds, and removes namespaces to a collection. XmlNamespaceManager also provides scope management for these namespaces. Because Books.xml uses the &quot;bk&quot; namespace in the code to follow, you must use XmlNamespaceManager.
 * The XPathNodeIterator class: This object provides an iterator over a set of selected nodes.

To create and run the Visual Basic .NET project, follow these steps: <ol> Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.</li> Place a Button control and a TextBox control on Form1.</li> Set the MultiLine property of the TextBox control to True.</li> Click to expand the TextBox control so that you can view four or five lines of data.</li>  Add the following code to the top of the Code window: Imports System.Xml Imports System.Xml.XPath </li>  To load the Books.xml file into an XmlDocument object, add the following code to the Button1_Click event: Dim oxmldoc As New XmlDocument oxmldoc.Load(&quot;c:\Books.xml&quot;) </li> Make sure that the Books.xml path in the preceding code points to the correct path on your computer.</li>  Use the CreateNavigator method of the XmlDocument object to create the XPathNavigator object so that you can run the XPath query: Dim oXPathNav As XPathNavigator oXPathNav = oxmldoc.CreateNavigator </li>  Use the Compile method of XPathNavigator to create an XPathExpression class, and then pass the XPath query as the parameter: Dim Expr As XPathExpression Expr = oXPathNav.Compile(&quot;//bk:Book[position>=2]&quot;) </li>  Use the the AddNamespace method to add the &quot;bk&quot; namespace to the XmlNamespaceManager object: Dim oxmlNSManager As New XmlNamespaceManager(oXPathNav.NameTable) oxmlNSManager.AddNamespace(&quot;bk&quot;, &quot;http://myserver/myschemas/Books&quot;) </li> <li> Use the SetContext method of XPathExpression to set the XPathExpression context to the XmlNamespaceManager: Expr.SetContext(oxmlNSManager) </li> <li> To run the XPath query and return the selected nodes, pass the expression to the Select method of the XPathNodeIterator: Dim iterator As XPathNodeIterator = oXPathNav.Select(Expr) While iterator.MoveNext TextBox1.Text = TextBox1.Text + vbCrLf + iterator.Current.Value End While </li> <li> The code in Button1_Click event should appear as follows: Dim oxmldoc As New XmlDocument Try oxmldoc.Load(&quot;c:\Books.xml&quot;)

Dim oXPathNav As XPathNavigator oXPathNav = oxmldoc.CreateNavigator

Dim Expr As XPathExpression Expr = oXPathNav.Compile(&quot;//bk:Book[position>=2]&quot;)

Dim oxmlNSManager As New XmlNamespaceManager(oXPathNav.NameTable) oxmlNSManager.AddNamespace(&quot;bk&quot;, &quot;http://myserver/myschemas/Books&quot;) Expr.SetContext(oxmlNSManager)

Dim iterator As XPathNodeIterator = oXPathNav.Select(Expr) While iterator.MoveNext TextBox1.Text = TextBox1.Text + vbCrLf + iterator.Current.Value End While

oxmlNSManager = Nothing oXPathNav = Nothing oxmldoc = Nothing

Catch ex As Exception MessageBox.Show(ex.Message) End Try </li> <li>Build and run the project.</li> <li>Click Button1. Notice that a list of books whose position is greater than or equal to 2 appears in the textbox.</li></ol>

back to the top

<div class="references_section">