Using XPath for Your XML Based Queries in C# Programming

XPath is one of the important elements in the W3C's XSLT standard and is often referred as a query language used to help us navigate through attributes, elements and other XML objects. It helps us identify the information and process the data in xml-based data sources in an efficient manner.

The rich System.XML namespace is loaded with many helper classes that help us to fiddle with xml. XPath defines a declarative syntax that is executed by a runtime (XPath Processor) to retrieve results. The result can be a set of XML nodes or a value buried deep amidst the tags. In fact, the XPath expressions follow a hierarchical notation that explains the relationship.

Following are the basic set of expressions that one can read through to get a hold of XPATH.

 

 

 

 

 

 

 

 

 

 

XPATH Expression

Description

/

To select the root node.

.

To select the current node.

//

To select the nodes from the current node

..

To select the parent of the current node

*

To match elements

@*

To match attributes

Predicates and XPath Wildcards are the next-level of syntactical concepts that you should know. Predicates allow you to figure out specific nodes based on an expression that is present in square braces. In other words, it helps you follow the rule of exclusion to derive a result. It is often compared to an IF/Else statement.

For example, look at the following xml.

<matches> 
      <match result="won" score="1"/>
      <match result="won" score="2"/>
      <match result="lost" score="5"/>
      <match result="draw" score="0"/>
</matches>

If you need to select the second match whose result was "won": you would write:

(//match)[@result=" won "][2] 

However, do remember that the order in which predicates are evaluated is of prime importance. The order in which they appear in an XPath expression that has a nested Predicate list, can affect your result. Each predicate follows the rule of being evaluated on the result of the immediate preceding predicate.

The next important XPath concept is the Wildcards. They are useful to select unknown xml elements.

The most widely used wildcards are the * wildcard - that matches any element node and the @* - that matches any attribute node.

An easy way to understand how these mighty expressions work is by writing more and more XPath expressions. The first reference that the developer community would recommend is the Free XPath Visualizer tool.

Also note that the examples on web, might only show you a two-node, three-element, four-attribute XML that will force you to think XML parsing is too easy. But in real life, XML is more complex and voluminous. Although XPath expressions solve every query, they have to carefully written and optimized for faster searches.

Interestingly, you may also want to know that when you work with binding sources that are XML based, you use XPath values to bind the data and not the conventional Path values.

Thanks for reading!

XPath functions

W3C Semantics

XPath Visualizer



About the Author

Srinath M S

I would love to leave a footprint in this flat world

Related Articles

Comments

  • Left Wanting More

    Posted by TexasJetter on 06/22/2011 07:39am

    This is an interesting topic, but I am left wanting more. You even admit that most examples on the web only show a two/three node XML, yours doesn't even go that far, and the XPath example is a single line. From this article I would have no clue as to how to setup and execute XPath expression. A full example would have been a great start to better explain XPath.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds