TextWriter serialize to UTF-8 string
<p>I use a TextWriter to serialize, because it's easy to switch between string serialization and file serialization:</p>

<p>Serialize to string;</p>

<pre><code>TextWriter stringWriter = new StringWriter();
XmlSerializer serializer = XmlSerializer(typeof(ObjectType))
serializer.Serialize(stringWriter , objetToSerialize)
return stringWriter.ToString();
</code></pre>

<p>Serialize to file;</p>

<pre><code>TextWriter fileWriter = new StreamWriter(targetXMLFileName, true, Encoding.UTF8);
XmlSerializer serializer = XmlSerializer(typeof(ObjectType))
serializer.Serialize(fileWriter , objetToSerialize)
fileWriter.Close();
</code></pre>

<p>My problem is that when serializing to string, it creates a UTF-16 ("?xml version="1.0" encoding="utf-16"?"), and TestWriter encoding property is ReadOnly</p>

<p>I have tried:</p>

<pre><code>var memoryStream = new MemoryStream();
TextWriter stringWriter = new StreamWriter(memoryStream, System.Text.Encoding.UTF8);
XmlSerializer serializer = XmlSerializer(typeof(ObjectType))
serializer.Serialize(stringWriter , objetToSerialize)
return stringWriter.ToString();
</code></pre>

<p>But it doesn't work. Instead of an XML Document it produces this string: "System.IO.StreamWriter" O_o</p>

<p>How can I initialize the TextWriter to UTF-8 Encoding</p>