troubleshooting Question

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

Avatar of steve_bagnall
steve_bagnall asked on
1 Comment1 Solution1123 ViewsLast Modified:

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

XmlDocument xmlDoc;
StringBuilder sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);

XslTransform xsl = new XslTransform();
xsl.Transform(xmlDoc, null, tw, null);  

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
 <xsl:output method="xml" version="1.1" encoding="utf-8" />
 <xsl:template match="/">
   <xsl:apply-templates />
 <xsl:template match="Node">
  <xsl:for-each select="Node">
   <xsl:sort select="@Text" order="ascending" />
    <xsl:attribute name="Text">
     <xsl:value-of select="@Text" />
    <xsl:attribute name="Value">
     <xsl:value-of select="@Value" />
    <xsl:apply-templates />

<?xml version="1.0" encoding="utf-8" ?>
 <Node Text="Root" Value='YYYYY' >
  <Node Text="XXXXXX" Value=YYYYYY' >
   <Node Text="XXX" Value='YYYY' >
    <Node Text="XXXXX" Value='YYYYYY' />
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