Groups | Blog | Home
all groups > dotnet xml > october 2005 >

dotnet xml : XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s


David Thielen
10/12/2005 8:21:01 PM
Hi;

I have an element:
<space> </space>

When I call SelectSingleNode() on it, the InnerXml is a 0 length String, not
a String containing 1 space.

Any ideas?

--
David Thielen
10/13/2005 6:47:03 AM
Hi;

Sorry - I forgot to mention - the API I give people is to pass in a
XPathNavigator. So it may not be an XmlDocument. And even if it is, I have no
way of accessing that object.

How do I do this for an XPathNavigator?

--
thanks - dave


[quoted text, click to view]
v-kevy NO[at]SPAM online.microsoft.com
10/13/2005 7:03:07 AM
Hi dave,

You can set PreserveWhitespace to true, so that the space will be preserved.

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.LoadXml("<space> </space>");
XmlNode n = doc.SelectSingleNode("space");
MessageBox.Show(n.InnerXml.Length.ToString());

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
David Thielen
10/13/2005 9:17:35 PM
Hi;

It's not my xml. I have a library that other people call and pass me their
xml. I wrote all of my code using XPathDocument & XPathNavigator because it
is the "suggested" approach.

But I need to have whitespace preserved. There must be a way as Microsoft
suggests using this approach and not using XmlDocument at all. Could you
please ask the .net team? I find it hard to believe they would not have made
it possible to do this.

--
thanks - dave


[quoted text, click to view]
v-kevy NO[at]SPAM online.microsoft.com
10/14/2005 3:17:54 AM
Hi dave,

There is no way to preserve whitespace in the code directly with
XPathNavigator. You can try to do the following:

1. Add whiteSpace="preserve" in the tag. <space whiteSpace="preserve">
</space>
2. If the node is a whitespace, check the XPathNavigator.NodeType property,
it has to be XPathNodeType.SignificantWhitespace or
XPathNodeType.Whitespace.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Chris Lovett
10/14/2005 9:45:31 AM
I'm on the .net xml team and I know for sure that once you've loaded an
XmlDocument without preserveWhitespace, you have lost all insignificant
whitespace information and there is no way for the XPathNavigator to
"reinvent" it on the fly. Therefore you'll have to tell folks who are
calling you not to do that. Or you chould change your component design so
that they call you to load the XML that way you can be in control of how the
whitespace processing happens.

[quoted text, click to view]

David Thielen
10/14/2005 10:31:10 AM
Hello;

First off, thank you for answering. Getting a definite no helps as then I
know that I'm not missing something.

Most cases I am passed a Stream so I can call new XmlDocument(Stream)
instead of new XPathDocument(Stream). But all of the documentation I have
read says that XPathDocument is a lot faster for heavy xpath use - so do I
then lose that efficiency?

And out of curiosity, why does XPathDocument eat all of the whitespace?
Because xml schemes like Excel's SpreadsheetML need it.

--
thanks - dave


[quoted text, click to view]
Chris Lovett
10/17/2005 1:39:45 PM
XPathDocument (and XmlDocument) default to preserveWhitespace=false for
backward compatibility reasons.

But you can tell both of these to preservice whitespace as follows:

new XPathDocument(@"..\\..\\xmlfile1.xml", XmlSpace.Preserve);

and
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;


[quoted text, click to view]

David Thielen
10/17/2005 1:51:08 PM
Yes - thank you.

I didn't see it for XPathDocument because the Stream ctor doesn't have that
option. But I can use the Stream to create an XmlDocument, Load it with the
Stream, then pass an XmlNodeReader.

--
thanks - dave


[quoted text, click to view]
AddThis Social Bookmark Button