Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i found it.</p> <p>The answer is no: The XmlDocument will not honor the ContentEncoding of the response stream it's writing to.</p> <hr> <p><strong>Update: the proper way to do it</strong></p> <ol> <li><p>Use <code>Response.Output</code>, and <strong>NOT</strong> <code>Response.OutputStream</code>.</p> <p>Both are streams, but <code>Output</code> is a <code>TextWriter</code>.</p> <p>When an <code>XmlDocument</code> saves itself to a <code>TextWriter</code>, it will use the encoding specified by the <code>TextWriter</code>. The <code>XmlDocument</code> will automatically change any xml declaration node, i.e.:</p> <p>&lt;?xml version="1.0" encoding="ISO-8859-1"?></p> <p>to match the encoding used by the <code>Response.Output</code>'s encoding setting.</p></li> <li><p>The <code>Response.Output</code> <code>TextWriter</code>'s encoding settings comes from the <code>Response.ContentEncoding</code> value.</p></li> <li><p>Use <code>doc.Save</code>, <strong>not</strong> <code>Response.Write(doc.ToString())</code> or <code>Response.Write(doc.InnerXml)</code></p> <p>You <strong>DON'T</strong> want to Save the xml to a string, or stuff the xml into a string, and <code>response.Write</code> that, because that: </p> <ul> <li>doesn't follow the encoding specified</li> <li>wastes memory</li> </ul></li> </ol> <p>To sum up: by Saving to a <code>TextWriter</code>: the XML Declaration node, the XML contents, and the HTML Response content-encoding will all match.</p> <p>Sample code:</p> <pre><code>public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState { //Note: We add IRequiesSessionState so that we'll have access to context.Session object //Otherwise it will be null public void ProcessRequest(HttpContext context) { XmlDocument doc = GetXmlToShow(context); //GetXmlToShow will look for parameters from the context if (doc != null) { context.Response.ContentType = "text/xml"; //must be 'text/xml' context.Response.ContentEncoding = System.Text.Encoding.UTF8; //we'd like utf-8 doc.Save(context.Response.Output); //doc save itself to the textwriter, using the encoding of the text-writer (which comes from response.contentEncoding) } #region Notes /* * 1. Use Response.Output, and NOT Response.OutputStream. * Both are streams, but Output is a TextWriter. * When an XmlDocument saves itself to a TextWriter, it will use the encoding * specified by the TextWriter. The XmlDocument will automatically change any * xml declaration node, i.e.: * &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; * to match the encoding used by the Response.Output's encoding setting * 2. The Response.Output TextWriter's encoding settings comes from the * Response.ContentEncoding value. * 3. Use doc.Save, not Response.Write(doc.ToString()) or Response.Write(doc.InnerXml) * 3. You DON'T want to Save the xml to a string, or stuff the xml into a string * and response.Write that, because that * - doesn't follow the encoding specified * - wastes memory * * To sum up: by Saving to a TextWriter: the XML Declaration node, the XML contents, * and the HTML Response content-encoding will all match. */ #endregion Notes } public bool IsReusable { get { return false; } } } </code></pre> <hr> <p>The encoding that the XmlDocument will use when saving to a stream depends on the encoding specified in the <strong>xml declaration node</strong>. e.g.:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; </code></pre> <p>If "UTF-8" encoding is specified in the xml declaration, then Save(stream) will use <strong>UTF-8</strong> encoding. </p> <p>If <em>no</em> encoding is specified, e.g.:</p> <pre><code>&lt;?xml version="1.0"?&gt; </code></pre> <p>or the xml declaration node is omitted entirely, then the XmlDocument will default to <strong>UTF-8</strong> unicode encoding. (<a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldeclaration.encoding.aspx" rel="noreferrer">Reference</a>)</p> <blockquote> <p>If an encoding attribute is not included, UTF-8 encoding is assumed when the document is written or saved out.</p> </blockquote> <p><a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldeclaration.encoding.aspx" rel="noreferrer">Some common encodings strings</a>, that you could also use in the xml declaration, are:</p> <ul> <li>UTF-8</li> <li>UTF-16</li> <li>ISO-10646-UCS-2</li> <li>ISO-10646-UCS-4 </li> <li>ISO-8859-1</li> <li>ISO-8859-2</li> <li>ISO-8859-3</li> <li>ISO-8859-4</li> <li>ISO-8859-5</li> <li>ISO-8859-6</li> <li>ISO-8859-7</li> <li>ISO-8859-8</li> <li>ISO-8859-9</li> <li>ISO-2022-JP</li> <li>Shift_JIS</li> <li>EUC-JP</li> </ul> <p><strong>Note</strong>: The encoding attribute is not case sensitive:</p> <blockquote> <p>Unlike most XML attributes, encoding attribute values are not case-sensitive. This is because encoding character names follow ISO and Internet Assigned Numbers Authority (IANA) standards.</p> </blockquote> <p>If you loaded your XML from a string or a file, and it did not contain an xml declaration node, you can <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldeclaration.encoding.aspx" rel="noreferrer">manually add one to the XmlDocument</a> using:</p> <pre><code>// Create an XML declaration. XmlDeclaration xmldecl; xmldecl = doc.CreateXmlDeclaration("1.0", null, null); xmldecl.Encoding="UTF-8"; // Add the new node to the document. XmlElement root = doc.DocumentElement; doc.InsertBefore(xmldecl, root); </code></pre> <p>If the XmlDocument does not have an xml declaration, or if the xml declaration does not have an encoding attribute, the saved document will not have one either.</p> <p>Note: If the XmlDocument is saving to a TextWriter, then the encoding that will be used is taken from the TextWriter object. Additionally, the xml declaration node encoding attribute (if present) will be replaced with the encoding of the TextWriter as the contents are written to the TextWriter. (<a href="http://msdn.microsoft.com/en-us/library/sa5wd18z.aspx" rel="noreferrer">Reference</a>)</p> <blockquote> <p>The encoding on the TextWriter determines the encoding that is written out (The encoding of the XmlDeclaration node is replaced by the encoding of the TextWriter). If there was no encoding specified on the TextWriter, the XmlDocument is saved without an encoding attribute.</p> </blockquote> <p>If saving to a string, the encoding used is determined by the xml declaration node's encoding attribute, if present.</p> <hr> <p>In my specific example, i am writing back to an Http client through ASP.NET. i want to set the Response.Encoding type to an appropriate value - and i need to to match what the XML itself will contain.</p> <p>The appropriate way to do this is to save the xml to the Response.Output, rather than the Response.OutputStream. The Response.Output is a TextWriter, who's Encoding value follows what you set for the Response.Encoding.</p> <p>In other words:</p> <pre><code>context.Response.ContentEncoding = System.Text.Encoding.ASCII; doc.Save(context.Response.Output); </code></pre> <p>results in xml:</p> <pre><code>&lt;?xml version="1.0" encoding="us-ascii" ?&gt; &lt;foo&gt;Hello, world!&lt;/foo&gt; </code></pre> <p>while:</p> <pre><code>context.Response.ContentEncoding = System.Text.Encoding.UTF8; doc.Save(context.Response.Output); </code></pre> <p>results in xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;foo&gt;Hello, world!&lt;/foo&gt; </code></pre> <p>and </p> <pre><code>context.Response.ContentEncoding = System.Text.Encoding.Unicode; doc.Save(context.Response.Output); </code></pre> <p>results in xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-16" ?&gt; &lt;foo&gt;Hello, world!&lt;/foo&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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