"We love the XPathReader" petition.

Oleg has been creating an implementation of a forward only XPath XmlReader which from a usability aspect has always been the nivana of streaming XML readers. This is similar to an example that I showed that Aaron published in Writing XML Providers for Microsoft .NET which implemented an XPathNavigator on top of an XmlReader. The hardest part about implementing one of these with this model is that the XPathNavigator is cloned regularly to be able to return to parent nodes to continue searching down a different branched of the tree. Of course it is not possible to clone an XmlReader since this means making a copy of the underlying stream. Even though this implementation supports a limited subset, it is still useful to think in terms of a path location rather than procedural code when accessing a document. Reading through this article remindered me of the difficulty of loading an XmlDocument when all you have is an XPathNavigator from your data source.This is a usability issue that has been address in V2 of System.Xml with a method called ReadSubTree(). Here is an example of splitting a single document up into many smaller documents for processing.

static void splitdocument()

{

      XmlDocument doc = new XmlDocument();

      doc.Load("books.xml");

      XPathNavigator nav = doc.CreateNavigator();

      XPathNodeIterator select = nav.Select("bookstore/book");

      while (select.MoveNext())

      {

            //Read the book subtree into an XmlDocument

            XmlDocument fragment = new XmlDocument();

            fragment.Load(select.Current.ReadSubtree());

            // Process the document here.

            Console.WriteLine(fragment.OuterXml);

      }

}

The life of forward only XPath has had a checkered history in System.Xml. In V1 we nearly shipped a Filter(string xpath) method on the XmlTextReader class that only reported nodes based upon a supplied XPath expression. This was cut. In V2 we determined the forward only subset of XPath and did the implementation work. This was cut, but survived as a Biztalk 2004 component which satisfied their scenarios very well. Finally as part of the soon to be launched XML developer center it will make it out into the real world as source code that you can use. Hopefully then you can send hundreds of “We love the XPathReader” mails to get it into the .NET framework.