title: Preserve xml formatting using XmlDocument
body: <p>I am using XmlDocument to work with xml</p>
  
  <p>How do I save my "XmlDocument" with my current formatting?</p>
  
  <p>Current formatting:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;root&gt;
  
    &lt;element&gt;&lt;/element&gt;
  
  &lt;/root&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>                XmlDocument testDoc = new XmlDocument();
                  testDoc.Load(@"C:\Test.xml");
  
                  **(do reading/writing using only XmlDocument methods)**
  
                  testDoc.Save(@"C:\Test.xml");
  </code></pre>
  
  <p>There is a similar topic:
  <a href="https://stackoverflow.com/questions/1684253/xmldocument-class-is-removing-formatting-c-net">XmlDocument class is removing formatting, c#, .NET</a></p>
  
  <p>The accepted answer is PreserveWhiteSpace = true, which in reality removes all whitespaces instead of preserving them.</p>
  
  <p><strong>Example:</strong></p>
  
  <p>Code:</p>
  
  <pre><code>    XmlDocument testDoc = new XmlDocument();
      testDoc.Load(@"C:\Test.xml");
      testDoc.PreserveWhitespace = true;
      testDoc.Save(@"C:\Test.xml");
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;root&gt;&lt;element&gt;&lt;/element&gt;&lt;/root&gt;
  </code></pre>