How to specify fully qualified element names in XPath queries by using Visual C++ .NET or Visual C++ 2005 (815677)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft XML Classes (included with the .NET Framework 1.1)
- Microsoft XML Classes (included with the .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 TASKSUMMARYThe 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
topCreate the XML file - Paste the following XML code in Notepad, or a text editor
of your choice:
<?xml version="1.0"?>
<bk:Books xmlns:bk='http://myserver/myschemas/Books'>
<bk:Book>
<bk:Title>Just XML</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>Professional XML</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>XML Step by Step</bk:Title>
</bk:Book>
<bk:Book>
<bk:Title>XML By Example</bk:Title>
</bk:Book>
</bk:Books> - On the File menu, click
Save.
- 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.
back to the topCreate the Visual C++
.NET or Visual C++ 2005 projectThe 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:
- 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.
- Add a Button control and a TextBox control to Form1.
- Set the MultiLine property of the TextBox control to True, and then set the Text property of the TextBox control to null.
- Adjust the size of the TextBox to hold
four or five lines of data.
- 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; - 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("c:\\Books.xml"); Note Make sure that the Books.xml path in this code points to
the correct path on your computer. - 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(); - 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("//bk:Book[position()>=2]"); - Use the AddNamespace method to add the bk namespace to the XmlNamespaceManager object:
XmlNamespaceManager* oxmlNSManager= new XmlNamespaceManager(oXPathNav.NameTable);
oxmlNSManager->AddNamespace("bk", "http://myserver/myschemas/Books"); - Use the SetContext method of XPathExpression to set the XPathExpression context to the XmlNamespaceManager:
Expr->SetContext(oxmlNSManager); - 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,"\r\n"),iterator->Current->Value); - The code in the Button1_Click event should appear as follows:
try{
XmlDocument* oxmldoc = new XmlDocument();
oxmldoc->Load("c:\\Books.xml");
XPathNavigator* oXPathNav;
oXPathNav = oxmldoc->CreateNavigator();
XPathExpression* Expr;
Expr = oXPathNav->Compile("//bk:Book[position()>=2]");
XmlNamespaceManager* oxmlNSManager= new XmlNamespaceManager(oXPathNav->NameTable);
oxmlNSManager->AddNamespace("bk", "http://myserver/myschemas/Books");
Expr->SetContext(oxmlNSManager);
XPathNodeIterator* iterator = oXPathNav->Select(Expr);
while (iterator->MoveNext())
{
this->textBox1->Text = System::String::Concat(System::String::Concat(this->textBox1->Text,"\r\n"),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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- 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.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:These steps apply to the whole article. - Press CTRL+SHIFT+S to save all the projects.
- Press CTRL+SHIFT+B to build the solution.
- Press CTRL+F5 to run the project.
- Click Button1. Notice that a list of books whose position is greater than or
equal to 2 appears in the text box.
back to the
topREFERENCES For
additional information, click the following article number to view the article
in the Microsoft Knowledge Base: 280457
PRB: Specifying Fully Qualified Element Names in XPath
back to the
top
Modification Type: | Major | Last Reviewed: | 1/18/2006 |
---|
Keywords: | kbWindowsForms kbcode kbHOWTOmaster KB815677 kbAudDeveloper |
---|
|