title: When saving an XmlDocument, it ignores the encoding in the XmlDeclaration (UTF8) and uses UTF16
body: <p>i have the following code:</p>
  
  <pre><code>var doc = new XmlDocument();
  
  XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  doc.AppendChild(xmlDeclaration);
  
  XmlElement root = doc.CreateElement("myRoot");
  doc.AppendChild(root);
  root.InnerText = "myInnerText";
  
  StringWriter sw = new StringWriter();
  doc.Save(sw);
  Console.WriteLine(sw.ToString());
  
  Console.WriteLine();
  
  MemoryStream ms = new MemoryStream();
  doc.Save(ms);
  Console.WriteLine(Encoding.ASCII.GetString(ms.ToArray()));
  </code></pre>
  
  <p>And here is the output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;myRoot&gt;myInnerText&lt;/myRoot&gt;
  
  ???&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;myRoot&gt;myInnerText&lt;/myRoot&gt;
  </code></pre>
  
  <p>Basically what it does is make an xml file, and set the encoding to utf8, but when it saves it to stringwriter it ignores my encoding and uses utf16. However, when using a memory stream, it uses utf8 (with the extra BOM chars)</p>
  
  <p>Why is this? Why isn't it honouring my explicit encoding setting of utf-8?</p>
  
  <p>Thanks a lot</p>