UTF-16 and XML
21 March 2006 in .Net, Code | Comments enabled
Part of a current project I’m working on involves creating an RSS feed. After developing an XSLT and the xml to be parsed it looked like everything was going to work out just fine. Loading it up in FireFox was OK however then I loaded it in IE 6.0… Error.
IE complained bitterly about trying to open the XML however everything else seemed to work. On closer inspection there was an issue with the XML declaration tag:
<?xml version="1.0" encoding="utf-16"?>
It should have been UTF-8, not UTF-16. At it turns out the StringWriter in .NET can only create UTF-16 (StringWriter has an encoding property on it however this is read-only). This was also despite changing the encoding in the XSLT:
<xsl:output omit-xml-declaration="no" method="xml" encoding="utf-8"/>
Of course it seems somewhat short sighted that Microsoft would develop .NET to default to encodings that their own products can’t even support. So after some hunting I’ve changed it to do this:
XmlWriter xtw = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
_xslTransform.Transform(doc, _argumentList, xtw, new XmlUrlResolver());
Now the encoding is correct and everyone is happy Not the most clever feature of .NET.