非Unicode代码 [英] Non-unicode XML representation
本文介绍了非Unicode代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个xml,其中一些元素值是unicode字符。是否可以用ANSI编码表示?
I have xml where some of the element values are unicode characters. Is it possible to represent this in an ANSI encoding?
Eg
<?xml version="1.0" encoding="utf-8"?>
<xml>
<value>受</value>
</xml>
到
<?xml version="1.0" encoding="Windows-1252"?>
<xml>
<value>殘</value>
</xml>
我反序列化XML,然后尝试使用XmlTextWriter序列化它,指定默认编码(默认为Windows- 1252)。所有的unicode字符最后作为问号。我使用VS 2008,C#3.5
I deserialize the XML and then attempt to serialize it using XmlTextWriter specifying the Default encoding (Default is Windows-1252). All the unicode characters end up as question marks. I'm using VS 2008, C# 3.5
推荐答案
好,我用下面的代码测试:
Okay I tested it with the following code:
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml><value>受</value></xml>";
XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.Default };
MemoryStream ms = new MemoryStream();
using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
XElement.Parse(xml).WriteTo(writer);
string value = Encoding.Default.GetString(ms.ToArray());
并正确转义unicode字符:
And it correctly escaped the unicode character thus:
<?xml version="1.0" encoding="Windows-1252"?><xml><value>受</value></xml>
我必须在别的地方做错事。感谢您的帮助。
I must be doing something wrong somewhere else. Thanks for the help.
这篇关于非Unicode代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文