I have a DataTable that I’m creating an XML file from using .WriteXML(..), although I have a problem with it exporting in UTF-16 encoding and there seems to be no apparent way of changing this. I understand that .NET uses UTF-16 internally within strings, is this correct?
I’m then running the XML that DataTable.WriteXML() produces through an XSLT that includes the following in the output declaration:
<xsl:output method="xml" indent="yes" encoding="utf-8" />
But still, the output from the transformation is in UTF16, and the system I am trying to input this XML file into doesn’t support UTF16.
Is there any way to force the output to UTF-8?
The encoding of the result-document is determined by the
encodingattribute of the<xsl:output>instruction — not by the XML declaration of the XML document that contains the XSLT transformation.Here is an example:
when applied on any XML document (not used in this simple demo), the result wanted is produced:
Do note: In .NET you might need to specify particular settings of the XmlWriter passed as parameter to the
XslCompiledTransform.Transform()method. See this for details how to specify the wanted encoding in theXmlWriterSettingsclass.