title: Force XDocument to write to String with UTF-8 encoding
body: <p>I want to be able to write XML to a String with the declaration and with UTF-8 encoding.  This seems mighty tricky to accomplish.</p>
  
  <p>I have read around a bit and tried some of the popular answers for this but the they all have issues.  My current code correctly outputs as UTF-8 but does not maintain the original formatting of the XDocument (i.e. indents / whitespace)!</p>
  
  <p>Can anyone offer some advice please?</p>
  
  <pre><code>XDocument xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), xelementXML);
  
  MemoryStream ms = new MemoryStream();
  using (XmlWriter xw = new XmlTextWriter(ms, Encoding.UTF8))
  {
      xml.Save(xw);
      xw.Flush();
  
      StreamReader sr = new StreamReader(ms);
      ms.Seek(0, SeekOrigin.Begin);
  
      String xmlString = sr.ReadToEnd();
  }
  </code></pre>
  
  <p>The XML requires the formatting to be identical to the way <code>.ToString()</code> would format it i.e.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;root&gt;
      &lt;node&gt;blah&lt;/node&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>What I'm currently seeing is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;&lt;root&gt;&lt;node&gt;blah&lt;/node&gt;&lt;/root&gt;
  </code></pre>
  
  <p><strong>Update</strong>
  I have managed to get this to work by adding <code>XmlTextWriter</code> settings... It seems VERY clunky though!</p>
  
  <pre><code>MemoryStream ms = new MemoryStream();
  XmlWriterSettings settings = new XmlWriterSettings();
  settings.Encoding = Encoding.UTF8;
  settings.ConformanceLevel = ConformanceLevel.Document;
  settings.Indent = true;
  using (XmlWriter xw = XmlTextWriter.Create(ms, settings))
  {
      xml.Save(xw);
      xw.Flush();
  
      StreamReader sr = new StreamReader(ms);
      ms.Seek(0, SeekOrigin.Begin);
      String blah = sr.ReadToEnd();
  }
  </code></pre>