迷惑编码和XslCompiledTransform [英] Messing with Encoding and XslCompiledTransform

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

问题描述

我搞砸了编码.

一方面,我有一个使用UTF-8响应的网址(非常确定,这要感谢Firebug插件).

For one hand i have a url that is responding me in UTF-8 (im pretty sure thanks to firebug plugin).

我使用以下代码打开读取UTF-8中内容的网址:

Im opening the url reading the content in UTF-8 using the following code:

StreamReader reader = new StreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8);

另一方面,我有一个转换xslt表,其中包含以下代码:

For other hand i have a transformation xslt sheet with the following code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
            <br/>
            hello
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

此xslt表也以UTF-8格式保存.

This xslt sheet is saved in UTF-8 format too.

我使用以下代码将xml与xslt混合:

I use the following code to mix the xml with the xslt:

StringWriter writer = new StringWriter();
XslCompiledTransform transformer = new XslCompiledTransform();

transformer.Load(HttpContext.Current.Server.MapPath("xslt\\xsltsheet.xslt");  
XmlWriterSettings xmlsettings = new XmlWriterSettings();
xmlsettings.Encoding = System.Text.Encoding.UTF8;
transformer.Transform(xmlreader, null, writer);   

return writer;

然后,我最终在Web浏览器中渲染了该编写器的内容,并且我收到了以下错误:

Then after all im render in the webbrowser the content of this writer and im getting the following error:

无法显示XML页面 无法使用样式查看XML输入 床单.请更正错误, 然后单击刷新按钮,或尝试 再过一遍.

The XML page cannot be displayed Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.

从当前编码切换到 不支持指定的编码. 错误处理资源 ' http://localhost:2541/Results ....

Switch from current encoding to specified encoding not supported. Error processing resource 'http://localhost:2541/Results....

<?xml version="1.0" encoding="utf-16"?>

我想知道在哪里可以找到UTF-16编码:

Im wondering where is finding the UTF-16 encoding take in count that:

  • 我所有的文件都保存为UTF-8
  • 服务器的响应是UTF-8
  • 读取xslt表的方式配置为UTF-8.

任何帮助将不胜感激.

谢谢.

最好的问候.

乔斯.

推荐答案

由于Encoding属性返回UTF-16编码,因此您的编写器导致将其写出.代替使用StringWriter(在内存中为UTF-16),您可以初始化XmlTextWriter实例以将UTF-8与MemoryStream用作后备存储.

Your writer is causing this to be written out, since the Encoding property returns the UTF-16 encoding. Instead of using a StringWriter (which is UTF-16 in memory) you could initialize an XmlTextWriter instance to use UTF-8 with a MemoryStream as backing store.

解决此问题的另一种方法是从StringWriter继承并覆盖Encoding属性以返回所需的编码(例如,您的情况下为UTF8).这个想法来自Robert McLaws撰写的博客帖子

Another way to work around the issue is to inherit from StringWriter and override the Encoding property to return the encoding you like (e.g. UTF8 in your case). This idea is from a blog post written by Robert McLaws.

public class UTF8StringWriter: StringWriter {
    public UTF8StringWriter() {}
    public UTF8StringWriter(IFormatProvider formatProvider): base(formatProvider) {}
    public UTF8StringWriter(StringBuilder sb): base(sb) {}
    public UTF8StringWriter(StringBuilder sb, IFormatProvider formatProvider): base(sb, formatProvider) {}

    public override Encoding Encoding {
        get {
            return Encoding.UTF8;
        }
    }
}

您并不孤单地遇到这个问题...例如,参见

You're not alone with this problem... see for instance the MSDN community comment (on the bottom) or the following blog post.

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

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