LINQ to XML works on top of XmlReader/XmlWriter. The XmlReader is an implementation of the XML processor/parser as described in the XML spec. That spec basically says that the parser needs to hide the actual representation in the text from the application above. Meaning that both \n and
should be reported as the same thing. That's what it does.
XmlWriter is the same thing backwards. It's purpose is to save the input in such a way, that when parsed you will get exactly the same thing back.
So writing a text value "\n" will write it such that the parser will report back "\n" (in this case the output text is \n for text node, but
for attribute due to normalization which occurs in attribute values).
Following that idea trying to write a text value "
" will actually write out " " because when the reader parses that it will get back the original "
".
LINQ to XML uses XmlWriter to save the tree to an XML file. So you will get the above behavior.
You could write the tree into the XmlWriter yourself (or part of it) in which case you get more control. In particular it will allow you to use the XmlWriter.WriteCharEntity method which forces the writer to output the specified character as a character entity, that is in the $#xXX; format. (Note that it will use the hex format, not the decimal).