Robert Strickland wrote:
I load an xml stream into a xpathdocument type, compile a xpath expression
and select to specific node. I find the node and wish to return the innerxml
of that node. When I use value of the current node I get back the "values"
of the child nodes but no formatted xml which I need for transformations
later in the sequence. Is there a way to pull pure xml from the
xpathdocument?
If you need access to the nodes as XmlNode objects then you need an
XmlDocument and not an XPathDocument, for instance try
using System;
using System.Xml;
using System.Xml.XPat h;
public class Test20031228 {
public static void Main (string[] args) {
XPathDocument xpathDocument = new XPathDocument(@ "test20031228.x ml");
XPathNavigator xpathNavigator = xpathDocument.C reateNavigator( );
TestXPathNaviga tor(xpathNaviga tor);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"test2003122 8.xml");
xpathNavigator = xmlDocument.Cre ateNavigator();
TestXPathNaviga tor(xpathNaviga tor);
}
static void TestXPathNaviga tor (XPathNavigator xpathNavigator) {
XPathExpression xpathExpression = xpathNavigator. Compile("/*");
XPathNodeIterat or nodeIterator =
xpathNavigator. Select(xpathExp ression);
while (nodeIterator.M oveNext()) {
xpathNavigator = nodeIterator.Cu rrent;
IHasXmlNode hasXmlNode = xpathNavigator as IHasXmlNode;
if (hasXmlNode != null) {
XmlNode currentNode = hasXmlNode.GetN ode();
Console.WriteLi ne(currentNode. InnerXml);
}
else {
Console.WriteLi ne("Can't access nodes.");
}
}
}
}
and you will find that the cast to IHasXmlNode (which has the GetNode
method) fails for the navigator created from the XPathDocument while it
works for the navigator created from the XmlDocument.
--
Martin Honnen
http://JavaScript.FAQTs.com/