Ziff-Davis Enterprise 
DevSource: Microsoft Developer Resource
Add OnsArchitectureLanguagesTechniquesUsing VSForums
 
Home arrow Architecture arrow Page 2 - Navigating XML Files in .NET
Navigating XML Files in .NET
By Jeff Cogswell

Rate This Article:
Add This Article To:
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");
ADVERTISEMENT

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.



 
 
>>> More Architecture Articles          >>> More By Jeff Cogswell
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog
Julia shows you some wicked cool use of LINQ!
MSDev Blog
Is the latest Delphi product, RAD Studio 2007, really necessary?
Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.