开发者

Messing with Encoding and XslCompiledTransform

开发者 https://www.devze.com 2022-12-30 12:33 出处:网络
im messing with the encodings. For one hand i have a url that is responding me in UTF-8 (im pretty sure thanks to firebug plugin).

im messing with the encodings.

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

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

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

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>

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

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.x开发者_C百科slt");  
XmlWriterSettings xmlsettings = new XmlWriterSettings();
xmlsettings.Encoding = System.Text.Encoding.UTF8;
transformer.Transform(xmlreader, null, writer);   

return writer;

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

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.

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

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

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

  • All my files are saved as UTF-8
  • The response from the server is in UTF-8
  • The way of read the xslt sheet is configured as UTF-8.

Any help would be appreciated.

Thanks in advance.

Best Regards.

Jose.


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.

Edit: 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.


Try to use:

StringBuilder stringBuilder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(stringBuilder, transformer.OutputSettings))
{
    xsl.Transform(xmlreader, writer);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号