XslCompiledTransform使用UTF-16编码 [英] XslCompiledTransform uses UTF-16 encoding

查看:161
本文介绍了XslCompiledTransform使用UTF-16编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,我想使用UTF-8编码格式输出XML数据。但它总是以UTF-16输出数据:

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));

推荐答案

XML输出​​将包含一个,基于该流,而不是在设置中指定的编码的编码标题。由于字符串是16位单code编码将是UTF-16。解决方法是晚饭preSS页眉和自己添加它来代替:

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;

然后,当你从StringBuilder的得到的结果:

Then when you get the result from the StringBuilder:

string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + sb.ToString();

这篇关于XslCompiledTransform使用UTF-16编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆