title: XML serializing with XmlWriter via StringBuilder is utf-16 while via Stream is utf-8?
body: <p>I was surprised when I encountered it, and wrote a console application to check it and make sure I wasn't doing anything else.</p>
  
  <p>Can anyone explain this?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>using System;    
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Xml;
  using System.Xml.Serialization;
  
  namespace ConsoleApplication1
  {
      public class Program
      {
          static void Main(string[] args)
          {
              var o = new SomeObject { Field1 = "string value", Field2 = 8 };
  
              Console.WriteLine("ObjectToXmlViaStringBuilder");
              Console.Write(ObjectToXmlViaStringBuilder(o));
              Console.WriteLine();
              Console.WriteLine();
              Console.WriteLine("ObjectToXmlViaStream");
              Console.Write(StreamToString(ObjectToXmlViaStream(o)));
              Console.ReadKey();
          }
  
          public static string ObjectToXmlViaStringBuilder(SomeObject someObject)
          {
              var output = new StringBuilder();
              var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };
  
              using (var xmlWriter = XmlWriter.Create(output, settings))
              {
                  var serializer = new XmlSerializer(typeof(SomeObject));
                  var namespaces = new XmlSerializerNamespaces();
  
                  xmlWriter.WriteStartDocument();
                  xmlWriter.WriteDocType("Field1", null, "someObject.dtd", null);
                  namespaces.Add(string.Empty, string.Empty);
                  serializer.Serialize(xmlWriter, someObject, namespaces);
              }
  
              return output.ToString();
          }
  
          private static string StreamToString(Stream stream)
          {
              var reader = new StreamReader(stream);
              return reader.ReadToEnd();
          }
  
          public static Stream ObjectToXmlViaStream(SomeObject someObject)
          {
              var output = new MemoryStream();
              var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };
  
              using (var xmlWriter = XmlWriter.Create(output, settings))
              {
                  var serializer = new XmlSerializer(typeof(SomeObject));
                  var namespaces = new XmlSerializerNamespaces();
  
                  xmlWriter.WriteStartDocument();
                  xmlWriter.WriteDocType("Field1", null, "someObject.dtd", null);
                  namespaces.Add(string.Empty, string.Empty);
                  serializer.Serialize(xmlWriter, someObject, namespaces);
              }
  
              output.Seek(0L, SeekOrigin.Begin);
  
              return output;
          }
  
          public class SomeObject
          {
              public string Field1 { get; set; }
              public int Field2 { get; set; }
          }
      }
  }
  </code></pre>
  
  <p>This is the result:</p>
  
  <p>ObjectToXmlViaStringBuilder</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;!DOCTYPE Field1 SYSTEM "someObject.dtd"&gt;
  &lt;SomeObject&gt;
  &lt;Field1&gt;string value&lt;/Field1&gt;
  &lt;Field2&gt;8&lt;/Field2&gt;
  &lt;/SomeObject&gt;
  </code></pre>
  
  <p>ObjectToXmlViaStream</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE Field1 SYSTEM "someObject.dtd"&gt;
  &lt;SomeObject&gt;
  &lt;Field1&gt;string value&lt;/Field1&gt;
  &lt;Field2&gt;8&lt;/Field2&gt;
  &lt;/SomeObject&gt;
  </code></pre>