tags:

views:

31

answers:

2

I have an XML element that looks something like this:

<content locale="en"> </content>

The text between the bracketed stuff consists of a single space character. When I load the XML into an XmlDocument look at the XmlElement object for the above element, I expect:

contentElement.InnerText.Length == 1; // InnerText should be a single space character

but instead what I get is

contentElement.InnerText.Length == 0;

Assuming this is not a bug in Microsoft's DOM implementation, is this a feature of the XML/DOM specification I'm not aware of? If so, do I have any options but to add escaped whitespace characters when the XML is written out?

+1  A: 

If you want to have whitespace preserved inside of your XML file after being parsed please consider using <![CDATA[ and ]]> section to let parser know that it should take this character data literally...

ŁukaszW.pl
That certainly works; thanks.
glaxaco
If it works then please accept my answer...
ŁukaszW.pl
A: 

I don't know about .net in particular, but the xml specifications are unclear about "whitespace nodes" (text nodes with nothing but whitespace. So implementations differ. Most libraries do have a method to start/stop (the opposite of the default) processing whitespace nodes.

The reason whitespace nodes do not have clearly defined behaviour is that you'll inadvertently have a lot of them when using a clean format:

<outerTag>
  <innerTag />
</outerTag>

That actually has two whitespace nodes!

Jasper