Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get XElement value with spaces?

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?

like image 942
Poma Avatar asked Feb 22 '23 21:02

Poma


1 Answers

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?

like image 60
Thomas Levesque Avatar answered Mar 08 '23 02:03

Thomas Levesque