Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET: Will Saving an XmlDocument to the Response.OutputStream honor the encoding?
    text
    copied!<p>i want to send the xml of an <code>XmlDocument</code> object to the HTTP client, but i'm concerned that the <a href="https://stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net/543431#543431">suggested soltuion</a> might not honor the encoding that the <code>Response</code> has been set to use:</p> <pre><code>public void ProcessRequest(HttpContext context) { XmlDocument doc = GetXmlToShow(context); context.Response.ContentType = "text/xml"; context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetAllowResponseInBrowserHistory(true); doc.Save(context.Response.OutputStream); </code></pre> <p>}</p> <p>What if i changed the encoding to something else, <strong>Unicode</strong> for instance:</p> <pre><code>public void ProcessRequest(HttpContext context) { XmlDocument doc = GetXmlToShow(context); context.Response.ContentType = "text/xml"; context.Response.ContentEncoding = System.Text.Encoding.Unicode; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetAllowResponseInBrowserHistory(true); doc.Save(context.Response.OutputStream); } </code></pre> <p>Will the <code>Response.OutputStream</code> translate the binary data that's being written to it on the fly, and make it Unicode?</p> <p>Or is the <code>Response.ContentEncoding</code> just <em>informative</em>?</p> <p>If the ContentEncoding is just informative, what content encoding will the follow text strings come back in?</p> <pre><code>context.Response.ContentEncoding = System.Text.Encoding.Unicode; context.Response.Write("Hello World"); context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.Write("Hello World"); context.Response.ContentEncoding = System.Text.Encoding.UTF16; context.Response.Write("Hello World"); context.Response.ContentEncoding = System.Text.Encoding.ASCII; context.Response.Write("Hello World"); context.Response.ContentEncoding = System.Text.Encoding.BigEndianUnicode; context.Response.Write("Hello World"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload