开发者

Output DataTable XML in UTF8 rather than UTF16

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 encoding attribute of the <xsl:output> instruction -- not by the XML declaration of the XML document that contains the XSLT transformation.

Here is an example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output encoding="utf-8"/>

 <xsl:template match="/">
   <t>Hello, world!</t>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used in this simple demo), the result wanted is produced:

<?xml version="1.0" encoding="utf-8"?><t>Hello, world!</t>

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 the XmlWriterSettings class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜