简体   繁体   中英

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.

You ensure this by using utf-8 encoding throughout the specification and handling of the xml document.

It's not the serialiser, it's the Console. Try Console.WriteLine("€"); or try 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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