|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article aims to explain what an XPath expression is and why they can be extremely useful to a C# programmer. BackgroundWhen I first started .NET programming I was immediately exposed to the use of XML It was everywhere, and I had hardly any exposure to it previously. After understanding why XML documents were used so widely, I took the decision to incorporate XML files into my next application. So off I went, added the Using XPath in C#Throughout this guide I will refer to the following XML file. <?xml version="1.0" encoding="utf-8" ?>
<books>
<book>
<title>A beginners guide to XPath</title>
<author>Gary Francis</author>
<description>A book that explains XPath for beginners</description>
<data type="Price">12.00</data>
<data type="ISBN">1234567890</data>
</book>
<book>
<title>Advanced C# Programming</title>
<author>A. Uther</author>
<description>Advanced applied C# techniques.</description>
<data type="Price">47.00</data>
</book>
<book>
<title>Understanding C# for beginners</title>
<author>Any body</author>
<description>How to get started with C# and .NET</description>
<data type="Price">12.00</data>
<data type="Comment">This was a great book... It helped Loads.</data>
<data type="Comment">Excellent material if you new to C#.</data>
</book>
</books>
The above XML file contains information about books. As you can imagine this file could be lot more complex, but for the sake of simplicity we will leave like this. Before we can do anything useful with this data, we need to create an XmlDocument document = null;
XmlNodeList nodeList = null;
XmlNode node = null;
// Try and load xml data into an Xml document object and throw an
// error message if this fails
try
{
document = new XmlDocument();
document.Load("Data.xml");
}
catch (Exception ex)
{
MessageBox.Show("Error loading 'Data.xml'. Exception: " + ex.Message);
}
In order for the above code to compile, you will need to reference the Now we have successfully loaded the data, we need to reference some data. To do this we will use an XPath expression. The data we are going to try and retrieve initially is a NodeList of all book elements within the XML file. The XPath expression to achieve this is: /books/book Don't worry too much that you don't know what this means as all will be revealed shortly. So we add the following code to the method: // Try and retrieve all book nodes
nodeList = document.SelectNodes("/books/book");
This will populate our XmlNodeList will all of the book elements. Well, within each of these elements we know there is going to be a <title> element. So we can use another XPath query to access that. The following code will achieve this for each of the book elements we just retrieved: foreach (XmlNode book in nodeList)
{
// Show a message with the book title
MessageBox.Show(book.SelectSingleNode("title").InnerText);
}
That was simple, right? So, right now, I bet you can imagine all sorts of ways you could use XPath in your applications, and you would be right. There is still the slight problem of the XPath syntax. Building XPath ExpressionsAs this is just a beginners tutorial, I will go over only the basic XPath expressions and what they mean. As time goes on you might want to try more complex XPath queries such as reading XML data in reverse (going from a child node to a parent node). This is outside the scope of this document, but there are plenty of other references on the internet that should be able to help. The first important thing you should remember when using XPath is the context you are in when you try to use the expression. For example if we used the <books>
<book>
<books>
<book></book>
</books>
</book>
</books>
From the above example, I hope you can understand the importance of context as this will become apparent when you try to put XPath to use in your applications. So the first thing we might need to know how to do using XPath is selecting some nodes. Here are some examples of how you can do this:
It is also important to note that you can use indexes when you want to select a particular node. For example Note: This is the W3C specification. In some browsers this is implemented incorrectly and they browser treats the indexes as zero based. This should only be a concern if you are copying code that was originally written for certain browsers. I believe IE5 and IE6 fall foul of this, but I have never investigated to be truthful, so this may or may not be the case. RecommendedI would seriously recommend that you download the source files for this project. It will allow you to play around with different XML data scenarios and XPath expressions easily and will help you to learn. I always say that it is better to learn from your mistakes than it is to not bother trying. ConclusionWell that is about it for my first contribution to Code Project. If you find this useful, or you would like me to write a follow up article which goes into some more detail on some of the more complex XPath expressions that might be needed drop me an e-mail. My address can be found on my Code Project profile page.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||