开发者

How to get XElement value with spaces?

I have following XElement:

<title>
  <bold>Foo</bold>
  <italic>Bar</italic>
</title>

开发者_Go百科When I get Value property it returns FooBar without space. How to fix it?


By definition, the Value of the <title> element is the concatenation of all text in this element. By default whitespace between elements and their contents is ignored, so it gives "FooBar". You can specify that you want to preserve whitespace:

var element = XElement.Parse(xml, LoadOptions.PreserveWhitespace);

However it will preserve all whitespace, including the line feeds and indentation. In your XML, there is a line feed and two spaces between "Foo" and "Bar"; how is it supposed to guess that you only want to keep one space?


From the documentation for the Value property of the XElement class:

Gets or sets the concatenated text contents of this element.

Given your example, this behavior is expected. If you want spaces, you will have to provide the logic to do it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜