4

I have a weird behavior with XElement. It seems the Value property changes the new line expression \r\n to the unix like expression \n. Why is that?

string valueString = "abc\r\ndef";
string xmlString = "<desc>abc\r\ndef</desc>";
XElement xElement = XElement.Parse(xmlString);

string toString = xElement.ToString();    //as expected same value as xmlString
string xElementValue = xElement.Value;    //contains abc\ndef instead of abc\r\ndef

Thanks!

pcius
  • 41
  • 2

1 Answers1

1

I believe this is by design - XML stores new line as LF, and not as Windows CR+LF.

If you think about it it's consistent with the principle that XML doesn't preserve white characters unless you have xml:space="preserve". Try adding that as the node attribute and see what you'll get.

veljkoz
  • 8,384
  • 8
  • 55
  • 91
  • You`re right it is by design in xml: http://www.w3schools.com/Xml/xml_syntax.asp. Also I tried using LoadOptions.PreserveWhitespace with the Parse method but I get the same behavior. Thanks a lot! – pcius Nov 18 '10 at 22:55
  • also, try adding it inside XML like abc\r\ndef and see if it helps – veljkoz Nov 19 '10 at 08:49