You are currently offline, waiting for your internet to reconnect

HOW TO: Specify Namespaces When You Use an XmlDocument to Execute XPath Queries in Visual Basic .NET

This article was previously published under Q316913
For a Microsoft Visual C# .NET version of this article, see 318545.

IN THIS TASK

SUMMARY
This step-by-step article demonstrates how to use the System.Xml.XmlDocument class to execute XPath queries against XML documents that define and use namespace prefixes to qualify element and attribute names.

back to the top

Create the XML Document

  1. Use Notepad to create a new XML document that contains the following code:
    <?xml version='1.0'?><Books xmlns="urn:Books" xmlns:pub="urn:Publisher"><Book>  <Title>Beginning XML</Title>  <pub:Publisher>Wrox</pub:Publisher></Book><Book>  <Title>XML Step by Step</Title>  <pub:Publisher>MSPress</pub:Publisher></Book><Book>  <Title>Professional XML</Title>  <pub:Publisher>Wrox</pub:Publisher></Book><Book>  <Title>Developing XML solutions</Title>  <pub:Publisher>MSPress</pub:Publisher></Book></Books>					
  2. Save the document as c:\books.xml in the root folder of your hard disk.
back to the top

Create the Visual Basic .NET Application

  1. Create a new Visual Basic .NET Windows Application project.
  2. Drag a command button control from the toolbox to the designer surface of Form1.vb.
  3. Paste the following code in the Click event procedure of the command button:
    'Instantiate an XmlDocument object.Dim xmldoc As New System.Xml.XmlDocument()'Load books.xml into the XmlDocument object. xmldoc.Load("c:\books.xml")'Instantiate an XmlNamespaceManager object. Dim xmlnsManager As New System.Xml.XmlNamespaceManager(xmldoc.NameTable)'Add the namespaces used in books.xml to the XmlNamespaceManager.xmlnsManager.AddNamespace("bk", "urn:Books")xmlnsManager.AddNamespace("pub", "urn:Publisher")Dim MSPressBookList As System.Xml.XmlNodeListDim MSPressBook As System.Xml.XmlNode'Execute the XPath query using the SelectNodes method of the XmlDocument.'Supply the XmlNamespaceManager as the nsmgr parameter.'The matching nodes will be returned as an XmlNodeList.'Use an XmlNode object to iterate through the returned XmlNodeList.MSPressBookList = xmldoc.SelectNodes("//pub:Publisher[. = 'MSPress']/parent::node()/bk:Title", xmlnsManager)For Each MSPressBook In MSPressBookList      System.Diagnostics.Debug.WriteLine(MSPressBook.InnerText)Next					
  4. Read the inline comments to understand the functionality of the code.
  5. The XmlNamespaceManager object is designed to serve as a collection of namespace definitions. It is used by the .NET Framework XML processors to resolve and manage the scope of namespaces used in XML documents. In the above example, pay specific attention to the following lines of code:
    xmlnsManager.AddNamespace("bk", "urn:Books")xmlnsManager.AddNamespace("pub", "urn:Publisher")					
  6. The AddNamespace method of an XmlNamespaceManager object is used to add a specified namespace to its namespace collection. It takes 2 parameters: the namespace prefix and the namespace URI. The above code is used to add the definitions of the two namespaces defined and used in Books.xml to an XmlNamespaceManager object. Books.xml overrides the default XML namespace (xmlns="urn:Books"), and defines a custom namespace (xmns:pub="urn:Publisher"). A custom user-defined prefix needs to be specified when adding an overridden default XML namespace URI to an XmlNamespaceManager collection. This prefix should then be used in XPath query expressions to qualify all element and attribute names that belong to the default XML namespace. In the sample code, the custom prefix bk is associated with the overriden default namespace URI. It is then used in the XPath query to qualify the Title element. (The Title element belongs to the overriden default XML namespace in books.xml.)
  7. The XmlNamespaceManager object is then supplied as the nsmgr (Namespace manager) parameter in the call to the SelectNodes method of the XmlDocument object to provide the namespace resolution and scope management required to successfully execute the specified XPath query. The XPath query in this sample is written to select the titles in Books.xml of all books published by MSPRESS.
back to the top

Test the Code

  1. Save the changes to the Visual Basic .NET project, and then run it.
  2. When the form is displayed, click the command button to execute the code. The following output listing the matching titles is displayed in the Visual Studio .NET Output window:
    XML Step by StepDeveloping XML solutions					
back to the top
REFERENCES
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
313828 INFO: Roadmap for Executing XPath Queries in .NET Applications
execute XPath queries
Properties

Article ID: 316913 - Last Review: 09/24/2003 18:36:48 - Revision: 3.3

  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
  • kbhowtomaster KB316913
Feedback