I have following XElement:
<title>
<bold>Foo</bold>
<italic>Bar</italic>
</title>
When I get Value
property it returns FooBar
without space. How to fix it?
I have following XElement:
<title>
<bold>Foo</bold>
<italic>Bar</italic>
</title>
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.