XPathDocument, XPathNavigator, XPathNodeIterator sample with C#
Section: XML WebservicesRating: Not rated yet!

Add to YiGGAdd to google-bookmarksAdd to linkarenaAdd to redditAdd to del.icio.usAdd to misterwongAdd to digg




Introduction

This sample shows how to use the XPathNavigator to with CreateNavigator to iterate over an Xml-Document loaded with XPathDocument.

This is the sample-Xml-document Books.xml:

<?xml version="1.0" encoding="utf-8" ?> <books> <book auhtor="Ernest Hemmingway">Fiesta</book> <book author="Unknown">The principles of software development</book> </books>

And here is the snippet to navigate the Xml-nodes and attributes:

private static void NavigateBooks() { XPathDocument docBooks = new XPathDocument("../../Books.xml"); XPathNavigator navBooks = docBooks.CreateNavigator(); XPathNodeIterator iterBooks = navBooks.Select("books/book"); foreach (XPathNavigator nodeBook in iterBooks) { Console.WriteLine("Found book: {0}", nodeBook.Value); Console.WriteLine("Author: {0}", nodeBook.GetAttribute("author", String.Empty)); } }




 Reader-Comments: