Messing with Encoding and XslCompiledTransform
        Posted  
        
            by Josemalive
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Josemalive
        
        
        
        Published on 2010-05-18T14:11:54Z
        Indexed on 
            2010/05/18
            14:30 UTC
        
        
        Read the original article
        Hit count: 675
        
Hello,
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.
© Stack Overflow or respective owner