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!

1 Answer 1

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.

2
  • You`re right it is by design in xml: 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, 2010 at 22:55
  • also, try adding it inside XML like <desc xml:space="preserve">abc\r\ndef</desc> and see if it helps
    – veljkoz
    Nov 19, 2010 at 8:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.