简体   繁体   English

XmlSerializer特殊字符

[英]XmlSerializer special characters

Currently I have the following code: 目前,我有以下代码:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            var myObject = new MyObject() {Text = "€ 232.22"};

            StringBuilder sb = new StringBuilder();
            var xmlWriterSettings = new XmlWriterSettings();


            XmlWriter writer = XmlWriter.Create(sb, xmlWriterSettings);
            new XmlSerializer(typeof(MyObject)).Serialize(writer, myObject);

            Console.WriteLine(sb.ToString());
            Console.ReadKey();
        }
    }
    [Serializable]
    public class MyObject
    {
        public MyObject()
        {
        }
        [XmlAttribute()]
        public string Text { get; set; }
    }
}

And the issue I have id that currently the serializer when i give it a euro symbol € it returns a ?, so then I tried passing € but it encodes the & and returns € 而且我有一个问题,即我给序列号序列化程序一个欧元符号€时,它返回一个?,因此我尝试传递€,但它对&进行编码并返回€。 Anyone know of an elegant way to solve this issue? 有人知道解决此问题的优雅方法吗?

Many thanks, 非常感谢,

Chris 克里斯

Ensure that you are using proper encoding style one the xml document it-self, as well as in your serializing and deserializing steps. 确保您自己使用了一种正确的编码样式,即xml文档本身,以及序列化和反序列化步骤。

You ensure this by using utf-8 encoding throughout the specification and handling of the xml document. 您可以在整个xml文档的规范和处理过程中使用utf-8编码来确保这一点。

It's not the serialiser, it's the Console. 不是序列化器,是控制台。 Try Console.WriteLine("€"); 试试Console.WriteLine("€"); or try Console.ReadKey(false); 或尝试Console.ReadKey(false); and then type € and you get the same results. 然后键入€,您将得到相同的结果。

(Assuming that is, that your console fonts don't have a €, which the default don't). (假设您的控制台字体没有€,而默认字体则没有)。

The € character isn't a "special character" in anyway, but a font can't handle ABC if it doesn't have glyphs for them. 无论如何,€字符都不是“特殊字符”,但是如果字体没有字形,则该字体将无法处理ABC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM