如何编码我的新Xml文档? [英] How Can I Encode My New Xml Document?

查看:117
本文介绍了如何编码我的新Xml文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我有一个带有XML文本的字符串,我想像XML一样保存它。我编码了字符串(到utf-8)但是当我想从那里制作XML时 - 我在Value中的西里尔符号显示不正确。我需要做什么来编码我的XML文档?



我的xml的一部分:

Hi all .
I have a string with a XML text and i want to save it like XML. I encoded string (to "utf-8") but when i want to make XML from that - my cyrillic symbols in Value don't displayed right . What i need to do to encode my XML document ?

part of my xml :

<rev:Code>Мои данные</rev:Code>





我的代码:



my code:

 string send = Encoding.GetEncoding("utf8").GetString(Encoding.GetEncoding("utf-8").GetBytes(send)); 
XmlDocument docsec = new XmlDocument();
 docsec.LoadXml(send);
docsec.Save("C:\\XmlNEW.xml");







原文:Моиданные



我在创建XML后看到它:МоиданнС<Рμ




Original text :Мои данные

I see it after creating XML :Мои данные

推荐答案

这条线路没有意义:

This line doesn't make sense:
string send = Encoding.GetEncoding("utf-8").GetString(Encoding.GetEncoding("utf-8").GetBytes(send));



你在.NET中看到字符串 Unicode的集合字符(或更好地说是 Unicode代码点的集合),表示由 UTF-16 编码编码的文本。

所以换句话说,你已经有了所需的文字表示。



现在我的公关esume可能导致您的问题是您指定了一个 XmlDeclaration 定义了一些其他(非utf-8)编码,或者可能是从您正在阅读的XML文本中读取的加载,或者你可能以其他方式保存文件,在这种情况下,问题可能与该代码有关。

如果您阅读 XmlDocument.Save(String)方法 [ ^ ]你会注意到它将使用取自<的一个编码属性code> XmlDeclaration.Encoding 属性。



尽管如此,您可以尝试以下方法:


You see in .NET String is a collection of Unicode characters (or in better words a collection of Unicode code points) that represent a text which is encoded by UTF-16 encoding.
So in other words you already have the desired text representation.

Now what I presume could cause your problem is either you have specified an XmlDeclaration that defines some other (non utf-8) encoding, or maybe that declaration is read from the XML text that you are loading, or maybe you are saving the file in some other manner in which case the problem is probably with that code.
If you read the remarks of XmlDocument.Save(String) Method[^] you will notice that it will use encoding attribute which is taken from the XmlDeclaration.Encoding property.

Nevertheless can you try the following:

XmlDocument docsec = new XmlDocument();
docsec.LoadXml(send);
using (TextWriter writer = new StreamWriter("C:\\XmlNEW.xml", false, Encoding.UTF8))
    docsec.Save(writer);



如果您的问题仍然存在,那么问题可能出在发送变量的内容本身,您应该调试它并查看它存储的内容。

这是一个想要的文本吗?如果没有,则问题在于检索该值的代码。


If your issue remains then the problem could be with the send variable's content itself, you should debug it and see what content it stores.
Is it a desired text? If not then the problem is with the code that is retrieving that value.


除了解决方案1:



内容XML也应该与实际应用的编码一致。使用UTF-8,这就是你的 prolog 的外观:

In addition to Solution 1:

The content of XML should also be consistent with the actually applied encoding. With UTF-8, this is how your prolog should look:
<?xml version="1.0" encoding="UTF-8"?>





这里有一个微妙的东西:BOM。首先,阅读它:

http://en.wikipedia.org/wiki/BOM

http://unicode.org/glossary

http://unicode.org/faq/utf_bom.html



使用Unicode的常规文本文件中的UTF由现代文本编辑器按BOM识别,仅由BOM识别。 XML处理器也总是使用它。但是对于UTF-8和prolog,即使没有BOM,也能正确检测到UTF。怎么样?这是因为当字符的所有代码点都落在代码点的ASCII范围内时(更确切地说,代码点32到127),UTF-8为您提供与ASCII完全相同的字节。因此,使用ASCII读取prolog,然后以指定的编码读取文档的其余部分。



在所有情况下,prolog和可选BOM必须与使用的实际编码。 :-)



-SA


这篇关于如何编码我的新Xml文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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