Navigating XML Files in .NET - ' Coding it ' (
Page 2 of 2 )
Navigating with XPaths
If you're opening an XML document for read-only, probably the easiest way to do so in .NET is using the XPathDocument class. The class includes a constructor that takes a filename as a parameter, for which you pass the name of an XML file, like so:
XPathDocument xpathdoc = new XPathDocument("myfile.xml");
Once you have the XML document open, you can create an instance of a class called XPathNavigator. This class lets you navigate about the document. Here's a complete example that demonstrates it:
using System;
using System.Xml.XPath;
namespace NavigateXML1 {
class Program {
static void Main(string[] args) {
XPathDocument xpathdoc = new XPathDocument("myfile.xml");
XPathNavigator pathnav = xpathdoc.CreateNavigator();
XPathNodeIterator iter = pathnav.Select("/customers/customer");
while (iter.MoveNext()) {
Console.WriteLine(iter.Current.GetAttribute("name", "));
Console.WriteLine(iter.Current.InnerXml);
Console.WriteLine("============================");
}
}
}
}
(To try out this example, save the earlier XML into a file called myfile, in the bin\debug directory of your project.)
This example uses the XPath
/customers/customer
Remember, this XPath refers to the collection of customer elements under the root customers element. In the case of our sample XML file, it refers to two separate elements.
To use this XPath, we call the Select method of our XPathNavigator instance. This method returns an object that you can use for iterating over the elements that match the XPath.
You can see in the code how I iterated over the set using the MoveNext method. (Oddly, you have to call MoveNext before retrieving the first element.)
After calling MoveNext, the XPathNodeIterator instance's Current member will contain the current element. (Technically, this "current element" is represented, in fact, by another instance of XPathNavigator. Yes, you read that right. It's beyond the scope of this article for me to explain why that is, but I encourage you to read the docs and play with the code to see exactly why this is and how it makes for a rather powerful tool.)
Once you have an element in an XPathNavigator instance, you can get information about the element. For example, you can obtain the values of the various attributes using the GetAttribute method. In the sample code earlier, I called GetAttribute, passing "name" so I could obtain the name element. (I also passed a second parameter, and empty string. If the attribute has a namespace attached to it, you pass the name space for the second parameter. If you're not sure what I'm talking about, don't worry about it yet; just pass an empty string.)
In this example, after obtaining the name element, I then obtain, as a string, the entire XML that's contained inside the current element. This is done through the InnerXml member. I then write this XML out to the console so you can see it.
More methods
The XPathNavigator.Select method is just one of many ways you can navigate an XML document, as well as modify the XML. Here are some other methods that you might want to check out:
SelectAncestors, SelectChildren, SelectDescendents: These methods are specialized forms of the Select method, as they only select either ancestors, children, or descendents.
MoveToChild, MoveToFirst, MoveToFirstChild, MoveToFollowing, MoveToParent, MoveToPrevious, MoveToRoot: These methods provide a more direct way to move about the document compared to the Select method.
InsertAfter, InsertBefore, InsertElementAfter, InsertElementBefore, PrependChild: Methods for inserting new nodes into the XML document. (Note, however, that if you open your XML document as an XPathDocument class, you cannot modify the document. To modify the document, you need to open it using the XmlDocument class.
Conclusion
If you're working with XML, the .NET framework can really make your life easier. Be sure to also check out the other classes for manipulating XML, as well as the XslTransform class.