i want to send the xml of an XmlDocument
object to the HTTP client, but i'm concerned that the suggested soltuion might not honor the encoding that the Response
has been set to use:
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);
}
What if i changed the encoding to something else, Unicode for instance:
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);
}
Will the Response.OutputStream
translate the binary data that's being written to it on the fly, and make it Unicode?
Or is the Response.ContentEncoding
just informative?
If the ContentEncoding is just informative, what content encoding will the follow text strings come back in?
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");
First 2 links from google
How to: Select an Encoding for ASP.NET Web Page Globalization: http://msdn.microsoft.com/en-us/library/hy4kkhe0.aspx
globalization Element (ASP.NET Settings Schema): http://msdn.microsoft.com/en-us/library/hy4kkhe0.aspx
i found it.
The answer is no: The XmlDocument will not honor the ContentEncoding of the response stream it's writing to.
Update: the proper way to do it
Use
Response.Output
, and NOTResponse.OutputStream
.Both are streams, but
Output
is aTextWriter
.When an
XmlDocument
saves itself to aTextWriter
, it will use the encoding specified by theTextWriter
. TheXmlDocument
will automatically change any xml declaration node, i.e.:<?xml version="1.0" encoding="ISO-8859-1"?>
to match the encoding used by the
Response.Output
's encoding setting.The
Response.Output
TextWriter
's encoding settings comes from theResponse.ContentEncoding
value.Use
doc.Save
, notResponse.Write(doc.ToString())
orResponse.Write(doc.InnerXml)
You DON'T want to Save the xml to a string, or stuff the xml into a string, and
response.Write
that, because that:To sum up: by Saving to a
TextWriter
: the XML Declaration node, the XML contents, and the HTML Response content-encoding will all match.Sample code:
The encoding that the XmlDocument will use when saving to a stream depends on the encoding specified in the xml declaration node. e.g.:
If "UTF-8" encoding is specified in the xml declaration, then Save(stream) will use UTF-8 encoding.
If no encoding is specified, e.g.:
or the xml declaration node is omitted entirely, then the XmlDocument will default to UTF-8 unicode encoding. (Reference)
Some common encodings strings, that you could also use in the xml declaration, are:
Note: The encoding attribute is not case sensitive:
If you loaded your XML from a string or a file, and it did not contain an xml declaration node, you can manually add one to the XmlDocument using:
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.
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. (Reference)
If saving to a string, the encoding used is determined by the xml declaration node's encoding attribute, if present.
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.
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.
In other words:
results in xml:
while:
results in xml:
and
results in xml: