Tuesday, August 16, 2005

Extracting XMLNode from XPathNodeIterator

The project that I am working on these days require me to play a lot with XML. To navigate XML and fetch required information I use XPath to query XML. Though XPath offers significant performance gains over XMLNode (DOM) based parsing. However, there are times when you just want to use XMLNode perhaps you want to get hold of OuterXML or want to append certain element to DocumentElement of XML document. In these scenarios XPathNodeIterator apparently is of no particular help, or so I thought atleast.
I was actually iterating an XML document using XPathNodeIterator and wanted to fetch the XMLNode instance for the current node that I was on. I really did not wanted to go for DOM based parsing and XPathNodeIterator wasnt helping me with XMLNode instance either. So I was virtually staring down the barrel ;) and dint know how to go about it. Then one of my juniors said "GOOGLE HAI NA!"... He googled it came up with this awesome post from Daniel Cazzulino.

After going through his post I was able to extract XMLNode from XPathNodeIterator. The catch was that XPathNavigator implements IHasXMLNode interface and you can type cast XPathNodeIterator.Current into IHasXMLNode and then call GetNode() to get instance of XMLNode.

Vist this link http://msmvps.com/hammadrajjoub/archive/2005/08/16/62851.aspx
to see my code snippet.

No comments: