13.3 Navigating XML Documents with XPath

 <  Day Day Up  >  

You want to select a single node or a group of nodes using an expression.


Technique

XPath is an expression-based language that uses path syntax to select nodes within an XML document. XPath itself is too large to explain in this section (let alone this book), so further reading might be required if you are unfamiliar with how to construct an XPath statement.

XPath expressions are used in the methods SelectSingleNode and SelectNodes defined within the XmlDocument class. These methods, when given an XPath expression, return a single XmlNode or XmlNodeList object, respectively. They allow you to navigate through an XML document in a random fashion rather than use forward-only techniques employed by the XmlReader -based classes.

The following code sets the Text property of a Label control based on a treeview item selected by the user . Because the TreeView control is hierarchically organized, you can programmatically generate an XPath expression from any node as shown in Figure 13.2, assuming the TreeView models the XML data contained within an XmlDocument object:

 
 private void tvXML_AfterSelect(object sender, System.Windows.Forms.   TreeViewEventArgs e) {     // clear attribute list view     lvAttributes.Items.Clear();     // query using XPath     XmlNode selNode = xmlDoc.SelectSingleNode(         tvXML.SelectedNode.FullPath, nsmanager );     // enumerate attributes in returned node and add to list view     foreach( XmlAttribute att in selNode.Attributes )     {         lvAttributes.Items.Add(new ListViewItem(             new string[]{att.Name, att.Value} ));     }     // set Label control Text to XPath expression     lblElementXPath.Text = tvXML.SelectedNode.FullPath;     lblAttXPath.Text = ""; } 
Figure 13.2. Using the FullPath property as XPath expressions.

graphics/13fig02.gif

Comments

Because XML is a hierarchical text-based file format, it seems natural to represent individual nodes within XML using path-based expressions. However, a simple path is only a portion of what XPath is capable of. In Figure 13.2, you can see the XPath expression that selects an attribute on an element on the bottom label. Although this list is by no means exhaustive, the following examples show different ways of selecting a node or nodes using XPath expressions. Let's assume that we have an XML document that looks like the following:

 
 <?xml version="1.0" encoding="ISO-8859-1"?> <catalog>   <cd id='0'>     <title>Blue Light, Red Light</title>     <artist>Harry Connick, Jr.</artist>     <country>USA</country>     <company>Sony/Columbia</company>     <price>14.99</price>     <year>1991</year>   </cd>   <cd id='1'>     <title>The Matrix</title>     <artist>Various</artist>     <price>14.99</price>     <year>1999</year>   </cd> </catalog> 

To select all the CD nodes in the XML document, use an XPath expression as a parameter to the SelectNodes method defined in the XmlDocument class:

 
 XmlNodeList cdNodes = xmlDoc.SelectNodes( "/catalog/cd" ); 

You can also use an indexer to retrieve just a single CD node. The following example uses the SelectSingleNode method to retrieve the first CD node in the XML document:

 
 XmlNode cdNode = xmlDoc.SelectSingleNode( "/catalog/cd[0]" ); 

In addition to selecting a group of nodes or a single node, you can also specify specific portions of a node. For instance, to select the text node of an element, you can use the XPath function text() . The following line only selects the text within the title node of the first CD:

 
 XmlNode textNode = xmlDoc.SelectSingleNode( "/catalog/cd[0]/title/text()" ); 

You can select attributes, as shown in Figure 13.2, by prefacing the attribute name with the @ symbol. Doing so selects the value for the attribute:

 
 XmlNode idNode = xmlDoc.SelectSingleNode( "/catalog/cd[0]/@id" ); 

XPath expressions can also use conditional statements for node selection. For example, if you want to select the CD whose id attribute is equal to 1 and the CDs whose price is greater than $10.00, you use the following expressions:

 
 XmlNode cdNode = xmlDoc.SelectSingleNode( "/catalog/cd[@id=='1']" ); XmlNodeList cdList = xmlDoc.SelectNodes( "/catalog/cd[price>'10.00']" ); 

Finally, XPath also allows you to select nodes using relative positioning. Whenever you construct an XPath expression, you can visualize a pointer into the document pointing to a node. That node then becomes your current axes point, which you can use as a basis for any relative node tests. For instance, if you want to find the CD that immediately followed the CD whose id attribute is 0, you use the following:

 
 XmlNode nextNode = xmlDoc.SelectSingleNode( "/catalog/cd[@id='0']/   following-sibling::*" ); 

Other types of node tests involve selecting a single child node, all descendants of a node, or all ancestors of a node, to name a few.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net