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

dotnet xml : 1 space node returns as empty


David Thielen
10/14/2005 4:40:02 PM
Hi;

This code is in J# so it looks a little weird but you could change it to C#
in a couple of seconds.

The problem here is a single space node returns as an empty node:

public static void main(String[] args) throws Exception
{

XmlDocument doc = new XmlDocument();
doc.set_PreserveWhitespace(true);

String xml = "<root><space> </space></root>";
Encoding encoder = Encoding.GetEncoding("utf-8");
doc.Load(new MemoryStream(encoder.GetBytes(xml)));

XPathNavigator nav = new XPathDocument(new
XmlNodeReader(doc)).CreateNavigator();
nav = nav.SelectSingleNode("/root");
nav = nav.SelectSingleNode("./space");
String str = nav.get_Value();
int len = str.length();
System.out.println("value = {" + str + "}");
System.out.println("length = " + len);
}

Any ideas how to get back the space that is the node value? I tried
doc.set_PreserveWhitespace(true); both before and after the Load.


--
Martin Honnen
10/15/2005 12:00:00 AM


[quoted text, click to view]


[quoted text, click to view]

When I try the following C#

string xmlMarkup = "<root><text> </text></root>";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(xmlMarkup);

XmlElement text = xmlDocument.SelectSingleNode(@"/root/text") as
XmlElement;
if (text != null) {
Console.WriteLine("InnerText: '{0}', Length: {1}.",
text.InnerText, text.InnerText.Length);

XPathNavigator xPathNavigator = text.CreateNavigator();
Console.WriteLine("Value: '{0}', Length: {1}",
xPathNavigator.Value, xPathNavigator.Value.Length);
}

with .NET 2.0 Beta 2 I get the output

InnerText: ' ', Length: 1.
Value: ' ', Length: 1

which seems fine to me, so maybe you can streamline your code.

Or do you have specific needs to
- create a memory stream from a string
- load an XmlDocument from that memory stream
- create a XmlNodeReader on that XmlDocument
- create an XPathDocument on that XmlNodeReader
- create an XPathNavigator from the XPathDocument
when loadXml exists for the first two steps and the XPathNavigator can
simply be created from an XmlNode?


--

Martin Honnen --- MVP XML
v-kevy NO[at]SPAM online.microsoft.com
10/15/2005 2:29:53 AM
Hi dave,

I don't quite understand the code. Does XpathNavigator has a method named
SelectSingleNode?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
David Thielen
10/15/2005 7:10:04 AM
Hello;

Yes, new to .net 2.0.

http://msdn2.microsoft.com/en-us/library/5htcxk90

--
thanks - dave


[quoted text, click to view]
David Thielen
10/15/2005 12:21:03 PM
Hi;

Interesting - I tried that and got that too (J#):
XmlElement elem = (XmlElement)doc.SelectSingleNode("/root/space");
String str = elem.get_InnerText(); // get a 1 space string

XPathNavigator nav = new XPathDocument(new
XmlNodeReader(doc)).CreateNavigator();
nav = nav.SelectSingleNode("/root/space");
String str = nav.get_Value(); // a 0 length string

Does XPathDocument just never allow whitespace? I hate to not be able to use
it as it is the recomended API and a lot faster. But I have to get the actual
node contents.

--
thanks - dave


[quoted text, click to view]
Oleg Tkachenko [MVP]
10/15/2005 11:29:33 PM
[quoted text, click to view]

When loading XPathDocument you have to preserve whitespace, by default
it's not preserved.
XPathNavigator nav = new XPathDocument(new
XmlNodeReader(doc), XmlSpace.Preserve).CreateNavigator();

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
Oleg Tkachenko [MVP]
10/15/2005 11:37:02 PM
[quoted text, click to view]

Second argument to XPathDocument is supposed to control whitespace
handling. By default it's XmlSpace.Default, which doesn't preserve
insignificant whitespace (whitespace-only text between tags).
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
David Thielen
10/16/2005 10:29:03 AM
Bingo - that's it.

I never looked at that ctor because I was using the XPathDocument(Stream)
ctor. But doing it as you suggest below works & slips right in.

--
thanks - dave


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