Messaging with encoding and XslCompiledTransform

im messing with encodings.

On the one hand, I have a url that answers me in UTF-8 (I'm pretty sure, thanks to the firebug plugin).

Im opens a URL reading the contents in UTF-8 using the following code:

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

On the other hand, I have an xslt conversion 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 also saved in UTF-8 format.

I use the following code to mix xml with 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, in the end, im renders the contents of this author in the webbrowser and im, receiving the following error:

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

Switching from the current encoding to the specified encoding is not supported. Error handling resource ' http: // localhost: 2541 / Results ....

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

I wonder where to find the UTF-16 encoding, assume that:

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

Any help would be appreciated.

Thanks in advance.

Best wishes.

Jose.

+4
source share
2 answers

Your writer calls this because the Encoding property returns UTF-16 encoding. Instead of using a StringWriter (which is UTF-16 in memory), you can initialize an XmlTextWriter instance to use UTF-8 with a MemoryStream as backup storage.

Edit: Another way around the problem is to inherit from StringWriter and override the Encoding property to return the encoding you need (for example, UTF8 in your case). This idea is taken from a message written by Robert MacLous .

 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 are not alone with this problem ... see, for example, the MSDN community comment (below) or the blog post below.

+9
source

Try using:

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

Source: https://habr.com/ru/post/1310121/


All Articles