Demon said:
Actually, Now I'm playing with it, using a Memory stream I can't get it
to
come out as anything other than UTF-8. Even if I change the encoding to
UTF-16 or anything else it still comes out as UTF-8 now. Any idea what's
happening?
Not really, you need to show some code and explain in detail where you
change the encoding, where you transform to, how you look at the result
then. For instance if you transform to a stream and then want to have a
string to look at you obviously need to read out the bytes in the stream
and convert them to a string.
Here is a short C# example:
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
public class Test2005090601 {
public static void Main (string[] args) {
string[] encodings = { "UTF-8", "UTF-16", "ISO-8859-1" };
XmlDocument xslStylesheet = new XmlDocument();
xslStylesheet.Load(@"test2005090601Xsl.xml");
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xslStylesheet.NameTable);
namespaceManager.AddNamespace("xsl",
"
http://www.w3.org/1999/XSL/Transform");
XmlAttribute encodingAttribute =
xslStylesheet.SelectSingleNode("/xsl:stylesheet/xsl
utput/@encoding",
namespaceManager) as XmlAttribute;
XslTransform xsltProcessor = new XslTransform();
foreach (string encoding in encodings) {
MemoryStream memStream = new MemoryStream();
encodingAttribute.Value = encoding;
xsltProcessor.Load(xslStylesheet);
xsltProcessor.Transform(xslStylesheet, null, memStream, null);
memStream.Position = 0;
byte[] resultBytes = new byte[memStream.Length];
memStream.Read(resultBytes, 0, (int)memStream.Length);
string resultString =
System.Text.Encoding.GetEncoding(encoding).GetString(resultBytes);
Console.WriteLine("Transformation result is: {0}.", resultString);
Console.WriteLine();
}
}
}
For simplicity I have choosen a simple stylesheet producing a static
result and run it against itself:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl
utput method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<test xml:lang="de">abcäöü?</test>
</xsl:template>
</xsl:stylesheet>
Now when I run that program in a Windows console the output is
Transformation result is: ?<?xml version="1.0" encoding="utf-8"?>
<test xml:lang="de">abcäöü?</test>.
Transformation result is: ?<?xml version="1.0" encoding="utf-16"?>
<test xml:lang="de">abcäöü?</test>.
Transformation result is: <?xml version="1.0" encoding="iso-8859-1"?>
<test xml:lang="de">abcäöü?</test>.
So the encoding in the XML declaration written to the stream is as
specified by the stylesheet in the <xsl
utput>.