Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XslCompiledTransform uses UTF-16 encoding

I have the following code, which I want to output xml data using the UTF-8 encoding format. but it always outputs data in UTF-16 :

        XslCompiledTransform xslt = new XslCompiledTransform();

            xslt.Load(XmlReader.Create(new StringReader(xsltString), new XmlReaderSettings()));

            StringBuilder sb = new StringBuilder();

            XmlWriterSettings writerSettings = new XmlWriterSettings();
            writerSettings.Encoding = Encoding.UTF8;
            writerSettings.Indent = true;

            xslt.Transform(XmlReader.Create(new StringReader(inputXMLToTransform)), XmlWriter.Create(sb, writerSettings));
like image 814
Attilah Avatar asked Mar 14 '11 18:03

Attilah


2 Answers

The XML output will contain a header that is based on the encoding of the stream, not the encoding specified in the settings. As strings are 16 bit unicode the encoding will be UTF-16. The workaround is to suppress the header and add it yourself instead:

writerSettings.OmitXmlDeclaration = true;

Then when you get the result from the StringBuilder:

string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + sb.ToString();
like image 94
Guffa Avatar answered Sep 21 '22 10:09

Guffa


If you use a MemoryStream in place of the StringBuilder, the XmlWriter will respect the encoding you specify in the XmlWriterSettings, since the MemoryStream doesn't have an inherent encoding like the StringBuilder does.

like image 30
Dave Andersen Avatar answered Sep 22 '22 10:09

Dave Andersen