title: How to serialize a string holding nothing but "\r\n" to XML correctly?
body: <p>We're using <code>DataContractSerializer</code> to serialize our data to XML. Recently we found a bug with how the string <code>"\r\n"</code> gets saved and read back - it was turned into just <code>"\n"</code>. Apparently, what causes this is using an <code>XmlWriter</code> with <code>Indent = true</code> set:</p>
  
  <pre><code>// public class Test { public string Line; }
  
  var serializer = new DataContractSerializer(typeof(Test));
  
  using (var fs = File.Open("C:/test.xml", FileMode.Create))
  using (var wr = XmlWriter.Create(fs, new XmlWriterSettings() { Indent = true }))
      serializer.WriteObject(wr, new Test() { Line = "\r\n" });
  
  Test test;
  using (var fs = File.Open("C:/test.xml", FileMode.Open))
      test = (Test) serializer.ReadObject(fs);
  </code></pre>
  
  <p>The obvious fix is to stop indenting XML, and indeed removing the "<code>XmlWriter.Create</code>" line makes the <code>Line</code> value roundtrip correctly, whether it's <code>"\n"</code>, <code>"\r\n"</code> or anything else. </p>
  
  <p>However, the way <code>DataContractSerializer</code> writes it still doesn't seem to be entirely safe or perhaps even correct - for example, just reading the resulting file with XML Notepad and saving it again destroys both <code>"\n"</code> and <code>"\r\n"</code> values completely.</p>
  
  <p>What is the correct approach here? Is using XML as a format for serializing binary data a flawed concept? Are we wrong to expect that tools like XML Notepad won't break our data? Do we need to augment each and every <code>string</code> field that could contain such text with some special attribute, perhaps something to force CDATA?</p>