title: Exclude XML directive from XslCompiledTransform.Transform output
body: <p>I'm using an <code>XsltCompiledTransform</code> to transform some XML into a fragment of HTML (not a complete HTML document, just a DIV that I will include in page generated elsewhere). </p>
  
  <p>I'm doing the transformation as follows: </p>
  
  <pre><code>StringBuilder output = new StringBuilder();
  
  XmlReader rawData = BusinessObject.GetXml();
  XmlWriter transformedData = XmlWriter.Create(output);
  
  XslCompiledTransform transform = new XslCompiledTransform();
  
  transform.Load("stylesheet.xslt");
  
  transform.Transform(rawData, transformedData);
  
  Response.Write(output.ToString());
  </code></pre>
  
  <p>My problem is that the result of the transform always begins with this XML directive:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  </code></pre>
  
  <p>How do I prevent this from appearing in my transformed data?</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>I'm telling the XSLT that I don't want it to output an xml declaration with</p>
  
  <pre><code>&lt;xsl:output method="html" version="4.0" omit-xml-declaration="yes"/&gt;
  </code></pre>
  
  <p>but this seems to have no effect on the directive appearing in my output. </p>
  
  <p>Interestingly, both my XML data source and my XSLT transform specify themselves as <code>UTF-8</code> not <code>UTF-16</code>.</p>
  
  <p><strong>UPDATE:</strong>
  The UTF-16 seems to be appearing because I'm using a string(builder) as an output mechanism. When I change the code to use a <code>MemoryStream</code> instead of a <code>StringBuilder</code>, my UTF-8 encoding is preserved. I'm guessing this has something to do with the internal workings of the <code>string</code> type and how it deals with encoding. </p>