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.  Following is a sample code that I used:

   1:  // Task XML Declaration
   2:              if(xpTaskXMLIterator.MoveNext())
   3:              {                    
   4:                  taskInfo    =    new Kalsoft.BusinessProcess.Core.Task();
   5:                  //taskInfo.TaskXml = xpTaskXMLIterator.Current.g
   6:                  int taskType=    int.Parse(xpTaskXMLIterator.Current.GetAttribute
   7:                      (Constants.Attributes.Type,
   8:                      xpTaskXMLIterator.Current.NamespaceURI));
   9:                  /// Task Type
  10:                  if(    taskType!=Kalsoft.BusinessProcess.Core.TaskType.EmailTask 
  11:                      &&    
  12:                      taskType!=Kalsoft.BusinessProcess.Core.TaskType.SpsDocLibTask )
  13:                  {
  14:                      throw new InvalidOperationException();
  15:                          
  16:                  }                                        
  17:                  taskInfo.Type    =    taskType;
  18:                  xmlNode            =    
  19:                  ((System.Xml.IHasXmlNode)xpTaskXMLIterator.Current).GetNode();
  20:                  
  21:                  taskInfo.TaskXml=    xmlNode.InnerXml;
  22:              }
 

Hammad Rajjoub,
MVP (Windows Server System - XML Web Services),
User Group Leader - Dot Net Wizards (
http://dotnetwizards.blogspot.com),
Chariman UG Relations Committee (
http://inetapakistan.org),
Member Speakers Bureau (
http://mea.ineta.org)