开发者

Question about XmlElement.InnerText and whitespace

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 implemen开发者_运维百科tation, 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?


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...


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜