Microsoft KB Archive/815677

= How to specify fully qualified element names in XPath queries by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815677

Article Last Modified on 1/18/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-







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



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

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Xml
 * System.Xml.XPath

IN THIS TASK

 * SUMMARY
 * Create the XML file
 * Create the Visual C++ .NET project
 * REFERENCES



SUMMARY
The step-by-step article describes how to specify fully-qualified element names in the NamespacePrefix:ElementName format to select nodes in an XmlDocument object.

back to the top

Create the XML file
  Paste the following XML code in Notepad, or a text editor of your choice:    Just XML   Professional XML </bk:Book>  XML Step by Step</bk:Title> </bk:Book>  XML By Example</bk:Title> </bk:Book> </bk:Books> </li> <li>On the File menu, click Save.</li> <li>In the Save As dialog box, click All Files in the Save as type box. In the File name text box, type Books.xml, and then click OK.</li></ol>

back to the top

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

To create and run the Visual C++ .NET or Visual C++ 2005 project, follow these steps: <ol> <li>Create a new Windows Forms Application project in Visual C++ .NET or in Visual C++ 2005. By default, a form that is named Form1 is added to the project.</li> <li>Add a Button control and a TextBox control to Form1.</li> <li>Set the MultiLine property of the TextBox control to True, and then set the Text property of the TextBox control to null.</li> <li>Adjust the size of the TextBox to hold four or five lines of data.</li> <li> On the View menu, click Code to open the Code window. Add the following code to the top of the Code window: using namespace System::Xml; using namespace System::Xml::XPath; </li> <li> To load the Books.xml file in an XmlDocument object, add the following code to the Click event of the Button control: XmlDocument* oxmldoc = new XmlDocument; oxmldoc->Load(&quot;c:\\Books.xml&quot;); Note Make sure that the Books.xml path in this code points to the correct path on your computer. </li> <li> Use the CreateNavigator method of the XmlDocument object to create the XPathNavigator object so that you can run the XPath query: XPathNavigator* oXPathNav; oXPathNav = oxmldoc->CreateNavigator; </li> <li> Use the Compile method of XPathNavigator to create an XPathExpression class, and then pass the XPath query as the parameter: XPathExpression* Expr; Expr = oXPathNav->Compile(&quot;//bk:Book[position>=2]&quot;); </li> <li> Use the AddNamespace method to add the bk namespace to the XmlNamespaceManager object: XmlNamespaceManager* oxmlNSManager= 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 to return the selected nodes, pass the expression to the Select method of the XPathNodeIterator: this->textBox1->Text = System::String::Concat(System::String::Concat(this->textBox1->Text,&quot;\r\n&quot;),iterator->Current->Value); </li> <li> The code in the Button1_Click event should appear as follows: try{ XmlDocument* oxmldoc = new XmlDocument; oxmldoc->Load(&quot;c:\\Books.xml&quot;); XPathNavigator* oXPathNav; oXPathNav = oxmldoc->CreateNavigator; XPathExpression* Expr; Expr = oXPathNav->Compile(&quot;//bk:Book[position>=2]&quot;); XmlNamespaceManager* oxmlNSManager= new XmlNamespaceManager(oXPathNav->NameTable); oxmlNSManager->AddNamespace(&quot;bk&quot;, &quot;http://myserver/myschemas/Books&quot;); Expr->SetContext(oxmlNSManager); XPathNodeIterator* iterator = oXPathNav->Select(Expr); while (iterator->MoveNext) {        this->textBox1->Text = System::String::Concat(System::String::Concat(this->textBox1->Text,&quot;\r\n&quot;),iterator->Current->Value); }    oxmldoc = NULL; oXPathNav = NULL; oxmlNSManager = NULL; } catch (Exception* exc) {    MessageBox::Show(exc->Message); } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> <li>Expand Configuration Properties, and then click General.</li> <li>Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article. </li> <li>Press CTRL+SHIFT+S to save all the projects.</li> <li>Press CTRL+SHIFT+B to build the solution.</li> <li>Press CTRL+F5 to 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 text box.</li></ol>

back to the top

<div class="references_section">