在UTF8而不是UTF16中输出DataTable XML [英] Output DataTable XML in UTF8 rather than UTF16

查看:136
本文介绍了在UTF8而不是UTF16中输出DataTable XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable,我正在使用.WriteXML(..)创建一个XML文件,尽管我以UTF-16编码导出问题,似乎没有明显的改变方式。我明白.NET在字符串内部使用UTF-16,这是否正确?



然后,我将通过XSLT运行DataTable.WriteXML()生成的XML,在输出声明中包含以下内容:

 < xsl:output method =xmlindent =yesencoding = utf-8/> 

但是,转换的输出仍然是UTF16,而我正在尝试输入这个XML文件不支持UTF16。



有没有办法强制输出到UTF-8?

解决方案

结果文档的编码由编码属性确定; xsl:output> 指令 - 不是通过包含XSLT转换的XML文档的XML声明。



这里是一个例子:

 < 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>

当应用于任何XML文档(未在此简单演示中使用)时,结果需要生成

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

请注意:在.NET中,您可能需要指定特定设置XmlWriter作为参数传递给 XslCompiledTransform.Transform()方法。请参阅 了解如何在 XmlWriterSettings 类中指定想要的编码。


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.

这篇关于在UTF8而不是UTF16中输出DataTable XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆