标签云

微信群

扫码加入我们

WeChat QR Code

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 resourceHow do I tell my website or page to use UTF-16 format.Thanks.Update: read both answers.


What is the exact line which throws the exception? Please, edit the question.

2019年04月19日35分25秒

The XML Parser inside Internet Explorer was throwing up the error because of the encoding format. So I think technically it was breaking on the very first few characters of the output. See my 4th or 5th comment on the accepted answer for what I had to do to get it Xml Serialized to UTF-8

2019年04月18日35分25秒

Actually I am using the ToXml Extension Method from here: 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-8

2019年04月19日35分25秒

Perhaps XmlSerializer.Serialize(Stream, Object) onto a byte stream, rather than (TextWriter, Object) is what's required?

2019年04月18日35分25秒

bobince, that is what the examples I saw online showed. Oh well few extra lines of code. thats all.

2019年04月18日35分25秒

Not exactly how I wrote it, but close enough: blog.newslacker.net/2008/02/net-xml-serialization.html and I used UTF8

2019年04月18日35分25秒

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"'.

2019年04月19日35分25秒