<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
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.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
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");

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 Microsoft Architecture Articles          >>> More By Jeff Cogswell