For Microsoft Visual Basic .NET versions of this article, see
301111.
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.xml system.xml.xpath
This task content
summary
Require how to use XPathnavigator classes to browse XML full code list troubleshooting reference
Summary This step-by-step guide describes how to use it from
XpathDocument object created
Xpathnavigator Objects Browse Scalable Markup Language (XML) documents. This presentation is loaded with XML data
XpathDocument object, create a view as a view
Xpathnavigator objects and display XML by traversing this document.
Back to top
Require the following list lists the recommended hardware, software, network infrastructure, and service packs required:
Microsoft Visual C # .NET This article assumes that you are familiar with the following topics:
XML Terminology Create and Read XML File XML Path Language (XPath) Syntax
Back to top
How to browse XML using XPathnavigator classes
Built a Visual C # .NET console application in Visual Studio. Net. Note: This example uses a file called Books.xml. You can create an example of your own Books.xml file or you can use the example of the .NET Software Development Kit (SDK). If you do not install "Quick Start" and don't want to install them, see the "Reference" section of the Books.xml download location. If you have already installed "Quick Start", Books.xml is located in the following folder: / program files / microsoft.net / frameworksdk / samples / quickstart / howto / samples / XML / TransformXML / VB must copy Books.xml to / bin / Debug folder, the folder is located in the folder you created in this project. Make sure the item references the System.xml namespace. Using the USING statement on XML and XPath namespace, this will not be required in the code to limit the declarations in these namespaces. The USING statement must be used before all other statements, as shown below: USING SYSTEM.XML;
USING SYSTEM.XML.XPATH; Declare the appropriate variable. Declare the XPathDocument object to save the XML document and declare the XPathNavigator object to calculate the XPath expression and traverse the document. Declare the String object to save the XPath expression. Add a declaration code during the MAIN of MAIN. Xpathnavigator NAV;
XpathDocument Docnav; load the XPathDocument object with sample file books.xml. The XPathDocument class uses Scalable Style Language Conversion (XSLT) to provide fast and performance cache for XML document processing. It is similar to the XML Document Object Model (DOM), but has been highly optimized for XSLT processing and XPath data models. // Open the xml.
DOCNAV = New XPathDocument (@ "c: /books.xml"); create an XPathnavigator object from a document. Xpathnavigator enables you to pass through the properties nodes and namespace nodes in the XML document. // Create a navigator to query with xpath.
NAV = docnav.createnavigator (); Using the MoveToroot method to move to the root of the document. MoveToroot puts the browser in the document node that contains the entire node tree. // Initial Xpathnavigator to Start at the root.nav.movetoroot (); Moved to the sub-level of the XML document using the MoveTofirstChild method. MoveTofirstChild method moves to the first child of the current node. For the source of Books.xml, the root document is moved from the root document to the sub-document, the "Comment" section and the BookStore node. //Move to the first child node (Comment Field).
Nav.movetofirstChild (); use the MoveToneXT method iteration through the node at the same level. Use the MoveToneXT method to move to the next level of the current node. // loop through all of the root nodes.
Do {
} While (Nav.Movetonext ()); Use the NodeType property to ensure that the node of the element is processed, using the Value property to display the text representation of the element. Do {
// Find the first element.
IF (nav.nodetype == xpathnottype.element) {
// determine WHether Children Exist.
IF (Nav.Haschildren == True) {
//Move to the first child.
nav.movetofirstchild ();
// loop through all the children.
Do {
// Display the data.
Console.write ("The XML String for this Child");
Console.WriteLine ("IS '{0}'", Nav.Value);
While (nav.movetonext ());
}
}
} while (nav.movetonext ()); use the Hasattributes property to determine if the node has any properties. Other methods (such as MoveTONEXTATTRIBUTE) can also be used to move to an attribute and check it. Note that the code segment is only traversed by the subsidy of the root node rather than the entire tree. Do {
// Find the first element.
IF (nav.nodetype == xpathnottype.element) {
// if Children EXIST
IF (Nav.Haschildren == True) {
//Move to the first child.
nav.movetofirstchild ();
// loop through all the children.
Do {
// Display the data.
Console.write ("The XML String for this Child");
Console.WriteLine ("IS '{0}'", Nav.Value);
// Check for attributes.
IF (nav.hasttributes == true) {
Console.writeline ("this node has attributes);
}
While (nav.movetonext ());
}
}
WHILE (Nav.Movetonext ()); Add PAUSE to the last READLINE method using the Console object to add PAUSE to it easily display the above results. //Pause.console.readline (); generates and runs Visual C # .NET project.
Back to top
Complete code list
Using system;
USING SYSTEM.XML;
Using system.xml.xpath;
Namespace q308343 {
Class class1 {
Static void main (string [] args) {
Xpathnavigator NAV;
XpathDocument docnav;
Docnav = new xpathdocument (@ "c: /books.xml");
NAV = docnav.createnavigator ();
Nav.movetoroot ();
//Move to the first child node (Comment Field).
nav.movetofirstchild ();
Do {
// Find the first element.
IF (nav.nodetype == xpathnottype.element) {
// determine WHether Children Exist.
IF (Nav.Haschildren == True) {
//Move to the first child.
nav.movetofirstchild ();
// loop through all of the children.
Do {
// Display the data.
Console.write ("The XML String for this Child");
Console.WriteLine ("IS '{0}'", Nav.Value);
// Check for attributes.
IF (nav.hasttributes == true) {
Console.writeline ("this node has attributes);
}
While (nav.movetonext ());
}
}
While (nav.movetonext ());
// pause.
Console.readline ();
}
}
}
Back to top
Troubleshooting When testing code, you may receive the following exception error message:
An Unhandled Exception of Type 'System.xml.xmlexception' Occurred In System.xml.dll
Additional Information: System Error.
This exception error occurs on the following code line:
DOCNAV = New XPathDocument ("C: //Books.xml"); the exception error is caused by invalid processing instructions. For example, the processing instructions may contain extra spaces. Here is an example of an invalid processing instruction:
XML Version = '1.0'?> To resolve this exception, do one of the following:
Correct invalid processing instructions. Below is an example of the effective handling instruction: XML Version = '1.0'> - or - an example below is a valid processing instruction: Remove the XML processing instruction from the books.xml file.
Back to top
Refer to the following files to download from the Microsoft Download Center:
Download Books.xml now For more information, visit the following Microsoft Web Site: XML IN .NET: .NET Framework XML Classes and C # Offer Simple, Scalable Data Manipulation (XML: .NET Framework XML class and C # provided Simple scalable data operation) r /> http://msdn.microsoft.com/msdnmag/issues/01/01/xml/xml.asp xpathnavigator class (xpathnavigator class) http://msdn.microsoft.com/ library / default.asp? url = / library / en-us / cpref / html / frlrfSystemXmlXPathXPathNavigatorClassTopic.asp XPathDocument class (XPathDocument class) http://msdn.microsoft.com/library/default.asp?url=/library/en -us / cpref / html / frlrfSystemXmlXPathXPathDocumentClassTopic.asp XSLT Transformations with the XslTransform class (XSLT conversion using XslTransform class) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide /html/cpconxslttransformationswithxsltransformclass.asp XPath examples (XPath example) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmrefxpathexamples.asp more information about XPath Please visit the following WWW Federation (W3C) Web site:
XML Path Language (XPATH) 1.0: W3C proposed in November 16, 1999 http://www.w3.org/tr/1999/rec-xpath-19991116
Back to top
The information in this article applies to:
Microsoft .NET framework class library Microsoft Visual C # .NET (2002)
Recent Updated: 2004-8-13 (2.2) Keywords: kbdownload kbhowtomaster kbxml KB308343