troubleshooting Question

XslTransform.Transform() returning UTF-16 encoded XML even when UTF-8 specified

Avatar of steve_bagnall
steve_bagnall asked on
ASP.NET
1 Comment1 Solution1123 ViewsLast Modified:
Hi,

I am using the XslTransform.Transform() method (see below) to try and sort XML data based on the value an attribute.  However, I have a problem in that the XML that is returned is encoded in UTF-16, and I require it at UTF-8.  I have attempted to force the issue with...

<xsl:output method="xml" version="1.1" encoding="utf-8" />

but this doesn't seem to work, any ideas?

Cheers, Steve


[C# CODE]
XmlDocument xmlDoc;
StringBuilder sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);

XslTransform xsl = new XslTransform();
xsl.Load("transform.xslt");                              
xsl.Transform(xmlDoc, null, tw, null);  


[XSLT STYLSHEET: TRANSFORM.XSLT]
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" version="1.1" encoding="utf-8" />
 <xsl:template match="/">
  <Tree>
   <xsl:apply-templates />
  </Tree>
 </xsl:template>
            
 <xsl:template match="Node">
  <xsl:for-each select="Node">
   <xsl:sort select="@Text" order="ascending" />
   <Node>
    <xsl:attribute name="Text">
     <xsl:value-of select="@Text" />
    </xsl:attribute>
    <xsl:attribute name="Value">
     <xsl:value-of select="@Value" />
    </xsl:attribute>
    <xsl:apply-templates />
                        
   </Node>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>


[SAMPLE XML]
<?xml version="1.0" encoding="utf-8" ?>
<Tree>
 <Node Text="Root" Value='YYYYY' >
  <Node Text="XXXXXX" Value=YYYYYY' >
   <Node Text="XXX" Value='YYYY' >
    <Node Text="XXXXX" Value='YYYYYY' />
   </Node>
  </Node>
...
</Tree>
      
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros