Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Messing with Encoding and XslCompiledTransform

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.xslt");  
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.

like image 270
Sosi Avatar asked Dec 09 '22 16:12

Sosi


2 Answers

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.

like image 185
Lucero Avatar answered Jan 02 '23 04:01

Lucero


Try to use:

StringBuilder stringBuilder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(stringBuilder, transformer.OutputSettings))
{
    xsl.Transform(xmlreader, writer);
}
like image 24
Gregoire Avatar answered Jan 02 '23 05:01

Gregoire