title: c# convert '\n\r' to '&#x13&#x10;' while saving XDocument
body: <p>I have searched for solution for this but all I found are questions how to prevent this.</p>
  <p>I have a simple code for saving and loading XML.</p>
  <pre><code>    XDocument xDoc;
  
      public Form1()
      {
          InitializeComponent();
          xDoc = new XDocument(new XElement(&quot;root&quot;, new XElement(&quot;data&quot;)));
      }
  
      private void textBox1_TextChanged(object sender, EventArgs e)
      {
          string input = textBox1.Text;
  
          xDoc.Root.SetElementValue(&quot;data&quot;, input);
  
          rtxtXPreview.Text = xDoc.ToString();
          txtValuePreview.Text = xDoc.Root.Element(&quot;data&quot;).Value;
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          SaveAndLoad();
      }
  
      private void SaveAndLoad()
      {
          string dataNodeValue;
          xDoc.Save(path);
          dataNodeValue = xDoc.Descendants(&quot;data&quot;).First().Value;
          txtValuePreview.Text = dataNodeValue;
          rtxtXPreview.Text = xDoc.ToString();
      }
  </code></pre>
  <p><strong>textBox1:</strong> is the input for the data element.</p>
  <p><strong>rtxtXPreview:</strong> is a rich textbox for displaying the text of the XML.</p>
  <p><strong>txtValuePreview:</strong> is a textbox for displaying the element  value.</p>
  <p>The problem is that when I enter value like:</p>
  <p>A</p>
  <p>B</p>
  <p>C</p>
  <p>The XML shows fine with the line breaks but once I hit the button the document is saved without those. So when I debugged my code I found that the characters \n\r is replaced with \n so I searched for this problem and found this <a href="https://stackoverflow.com/questions/3684032/linq-to-xml-generates-r-n-between-tags">code</a>:</p>
  <pre><code>    private void SaveAndLoadFixed()
      {
          string dataNodeValue;
  
          XmlWriterSettings xmlWriterSettings =
              new XmlWriterSettings { NewLineHandling = NewLineHandling.None, Indent = true };
  
          using (XmlWriter xmlWriter = XmlWriter.Create(path, xmlWriterSettings))
          {
              xDoc.Save(xmlWriter);
              xmlWriter.Flush();
          }
  
          //xDoc.Save(path);
  
          XElement xDatabaseElement;
          using (XmlTextReader xmlTextReader = new XmlTextReader(path) { WhitespaceHandling = WhitespaceHandling.Significant })
          {
              xmlTextReader.MoveToContent();
              xDatabaseElement = XElement.Load(xmlTextReader);
          }
  
          dataNodeValue = xDatabaseElement.Descendants(&quot;data&quot;).First().Value;
          txtValuePreview.Text = dataNodeValue;
          rtxtXPreview.Text = xDatabaseElement.ToString();
      }
  </code></pre>
  <p>This worked fine but why the characters <code>\n\r</code> not replaced with <code>&amp;#x13&amp;#x10;</code> because when I tried to type the character <code>&lt;</code> or <code>&gt;</code> it was replaced with <code>&amp;lt;</code> &amp; <code>&amp;gt;</code> respectively ?</p>