469,575 Members | 1,206 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

XPathNodeIterator get outerXml?

KJ
It is true that there is no way to get the outerXml of
XPathNodeIterator.Current? Please say it isn't so.

Nov 17 '05 #1
3 5147


KJ wrote:
It is true that there is no way to get the outerXml of
XPathNodeIterator.Current? Please say it isn't so.


That depends on the type of document you have created the XPathNavigator
over, if it is an XmlDocument then you can do it:

XmlDocument doc = new XmlDocument();
doc.Load("books.xml");

// Create an XPathNavigator and select all books by Plato.
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator ni =
nav.Select("descendant::book[author/name='Plato']");
ni.MoveNext();

// Get the first matching node
XmlNode book = ((IHasXmlNode)ni.Current).GetNode();
Console.WriteLine(book.OuterXml);

But that cast to IHasXmlNode will fail for an navigator created over an
XPathDocument.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 17 '05 #2

It depends. XPathNodeIterator.Current returns an XPathNavigator
instance represented by the current node within the iteration.
XPathNavigator is an abstract class and by itself does not provide
OuterXml support.

However, one of the implementation sof XPathNavigator is
DocumentXPathNavigator which implements IHasXmlNode which expose
GetNode() which returns the corresponding XmlNode instance. This does
have an OuterXml property.

So to conditionally get OuterXml you can do something like

XPathNavigator current = iterator.Current;
string outerXml;

if (current != null && current is IHasXmlNode) {
outerXml = ((IHasXmlNode)current).GetNode().OuterXml;
} else {
outerXml = "<unavailable>";
}

Whether or not the XPathNavigator implementation is a
DocumentXPathNavigator or something else depends on what the
XPathNodeIterator is iterating over in the first place--if it
originally came from an XmlDocument then it will be
DocumentXPathNavigator, but if you're iterating something else, then
the current node implementation will be different.

HTH,

Sam

On 20 May 2005 08:31:03 -0700, "KJ" <n_**********@mail.com> wrote:
It is true that there is no way to get the outerXml of
XPathNodeIterator.Current? Please say it isn't so.


Nov 17 '05 #3
KJ
I used an XPathDocument to create the navigator

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Ilian Nilsson | last post: by
2 posts views Thread by avnrao via .NET 247 | last post: by
2 posts views Thread by Emmanuel | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.