Microsoft KB Archive/318545

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

Article ID: 318545

Article Last Modified on 5/23/2005

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q318545



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

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

REFERENCES



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
  Use Notepad to create a new XML document that contains the following code:    Beginning XML Wrox   XML Step by Step MSPress</pub:Publisher> </Book>  Professional XML</Title> Wrox</pub:Publisher> </Book>  Developing XML solutions</Title> MSPress</pub:Publisher> </Book> </Books> </li> <li>Save the document as c:\books.xml.</li></ol>

back to the top

Create the Visual C# .NET Application
<ol> <li>Create a new Visual C# .NET Windows Application project.</li> <li>Drag a command button control from the toolbox to the designer surface of Form1.cs.</li> <li> Paste the following code in the Click event procedure of the command button: //Instantiate an XmlDocument object. System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument;

//Load books.xml into the XmlDocument object. xmldoc.Load(&quot;C:\\books.xml&quot;);

//Instantiate an XmlNamespaceManager object. System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xmldoc.NameTable);

//Add the namespaces used in books.xml to the XmlNamespaceManager. xmlnsManager.AddNamespace(&quot;bk&quot;, &quot;urn:Books&quot;); xmlnsManager.AddNamespace(&quot;pub&quot;, &quot;urn:Publisher&quot;);

System.Xml.XmlNodeList MSPressBookList;

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

foreach (System.Xml.XmlNode MSPressBook in MSPressBookList) {   System.Diagnostics.Debug.WriteLine(MSPressBook.InnerText);

}                   </li> <li>Read the inline comments to understand the functionality of the code.</li> <li> 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(&quot;bk&quot;, &quot;urn:Books&quot;) xmlnsManager.AddNamespace(&quot;pub&quot;, &quot;urn:Publisher&quot;) </li> <li>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=&quot;urn:Books&quot;), and defines a custom namespace (xmns:pub=&quot;urn:Publisher&quot;). 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.)</li> <li>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.</li></ol>

back to the top

Test the Code
<ol> <li>Save the changes to the Visual C# .NET project, and then run it.</li> <li> 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 Step Developing XML solutions </li></ol>

back to the top

<div class="references_section">