更改XML文件的编码类型 [英] Changing the encoding type of XML file

查看:692
本文介绍了更改XML文件的编码类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想将XML文件的编码类型更改为UTF-8.我尝试读取文件并使用UTF-8进行写回,但它写回了一个空文件,因为我不知道文件的默认编码.

请帮帮我.

问候

Hello,

I want to change the encoding type of XML file to UTF-8. I tried reading the file and write it back with UTF-8 but its written an empty file back, Because I dont know the default encoding of a file.

Kindly help me out.

regards

推荐答案

您是说您拥有的XML(源)未指定编码类型吗?

您是否尝试过检查(希望)使用读取源的XmlDocument的编码类型:XmlDeclaration.Encoding get/set属性:如果执行此操作会发生什么?

如果您的源XML是.NET生成的,并且未指定编码属性:在读取或写入XML时,UTF-8将是默认值.

假设您已使用''StringReader或其他方式读取XML,并且已创建了有效的" XmlDocument,则在将其写为UTF-8时,应创建一个XmlDeclaration实例,设置该实例的XmlEncoding属性,并将其添加到XmlDocument的根节点.

这显示在MSDN上的代码示例中:[ ^ ].

现在,如果您的源XML处于某种奇怪的格式,您将不知道,也无法确定:也许这意味着您将不得不自己弄清楚它的结构,并使用XSLT来使用一种可用的形式. ?

Bill,祝你好运
Are you saying the XML you have (the source) does not specify an Encoding Type ?

Have you tried examining the Encoding Type of the XmlDocument your source is (hopefully) read into using : XmlDeclaration.Encoding get/set Property: what happens if you do ?

If your source XML is .NET generated, and does not specify an encoding attribute: UTF-8 will be the default in either reading or writing out the XML.

Assuming you have read in the XML using ''StringReader or whatever, and you have a valid ''XmlDocument created, when writing it out, as UTF-8, you should create an XmlDeclaration instance, set the XmlEncoding property of that instance, and add it to the root node of the XmlDocument.

This is shown in the code example here on MSDN:[^].

Now if your source XML is in some weird format, that you don''t know, and can''t determine: perhaps that mean you are going to have to figure out its structure yourself and use an XSLT to get into a usable form ?

good luck, Bill


这篇关于更改XML文件的编码类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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