How To Specify Fully Qualified Element Names in XPath Queries by Using Visual C# .NET (313188)
The information in this article applies to:
- Microsoft Visual C# .NET (2002)
- Microsoft Visual C# .NET (2003)
- Microsoft XML Classes (included with the .NET Framework 1.0)
- Microsoft XML Classes (included with the .NET Framework 1.1)
This article was previously published under Q313188 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 TASKSUMMARY The step-by-step article shows you 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- 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:
<?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, in the Save as type text box, click All Files. In the File name text box, type
Books.xml, and then click OK.
back to the top
Create the Visual C# .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 "bk" 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 C# .NET project, follow these
steps:
- Create a new Windows Application project in Visual C# .NET.
Form1 is added to the project by default.
- Place a Button control and a TextBox control on Form1.
- Set the MultiLine property of the TextBox control to True.
- Click to expand the TextBox control so that you can view four or five lines of
data.
- Add the following code to the top of the Code window:
using System.Xml;
using System.Xml.XPath;
- To load the Books.xml file into an XmlDocument object, add the following code to the Click event of the Button control:
XmlDocument oxmldoc = new XmlDocument();
oxmldoc.Load(@"c:\Books.xml");
- Make sure that the Books.xml path in the preceding 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 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 return the selected nodes, pass
the expression to the Select method of the XPathNodeIterator:
XPathNodeIterator iterator = oXPathNav.Select(Expr);
while (iterator.MoveNext())
{
this.textBox1.Text = this.textBox1.Text + "\r\n"+ iterator.Current.Value ;
}
- The code in Button1_Click event should appear as follows:
XmlDocument oxmldoc = new XmlDocument();
try
{
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 = this.textBox1.Text + "\r\n"+ iterator.Current.Value ;
}
oxmlNSManager = null;
oXPathNav = null;
oxmldoc = null;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
- Build and run the project.
- Click Button1. Notice that a list of books whose position is greater than or
equal to 2 appears in the textbox.
back to the top
REFERENCESFor
additional information, click the article number below to view the article in
the Microsoft Knowledge Base: 280457 PRB: Specifying Fully Qualified Element Names in XPath Queries
back to the top
Modification Type: | Major | Last Reviewed: | 1/13/2006 |
---|
Keywords: | kbHOWTOmaster KB313188 kbAudDeveloper |
---|
|