How did you get ASP.NET to output UTF-16 encoded text?
I serialize an object in .NET which by default is UTF-16 format. Now I want to send the string as an output response to an .ashx request.
I get the error: Switch from current encoding to specified encoding not supported. Error processing resource
How do I tell my website or page to use UTF-16 format.
Thanks.
Update: read both answers.
-
Change the
Response.ContentEncoding
property toSystem.Text.Encoding.Unicode
? -
In general: don't.
Web pages as UTF-16 confuse many tools and make browsers behave in odd, unexpected ways. (Esp.: linked scripts, form submissions, proxies). UTF-16 can only be served safely as a binary object, so for web content stick to an encoding that is a superset of ASCII - the obvious choice being UTF-8.
The error you quote is generally what you get when you try to read an XML file whose <?xml encoding="..."?> prolog clashes with the real encoding of the file. None of the superset-of-ASCII encodings ever clash because 'xml encoding' is ASCII and represented the same in all of them, but if you've put encoding="utf-16" in an XML file that's not saved as UTF-16, or you've not put it in when it is, then the parser can't cope with the logical impossibility.
tyndall : Actually I am using the ToXml Extension Method from here: http://solidcoding.blogspot.com/2007/11/c-toxml-extension-method.html .Since System.String is stored internally as UTF-16 that is where my problem is. I will have to use a longer ToXml method that accounts 4 this and converts to UTF-8bobince : Perhaps XmlSerializer.Serialize(Stream, Object) onto a byte stream, rather than (TextWriter, Object) is what's required?tyndall : @bobince, that is what the examples I saw online showed. Oh well few extra lines of code. thats all.tyndall : Not exactly how I wrote it, but close enough: http://blog.newslacker.net/2008/02/net-xml-serialization.html and I used UTF8bobince : That example too is using the TextWriter. I haven't tried it, but it seems to me like the method accepting a Stream instead should be byte-oriented and hence not produce a spurious 'encoding="UTF-16"'.
0 comments:
Post a Comment