0
[XmlRoot("Class1")]
class Class1
{
[(XmlElement("Product")]
public string Product{get;set;}
[(XmlElement("Price")]
public string Price{get;set;}
}

これは私のクラスです。この場合、価格には「£」記号が含まれています。XML にシリアル化した後、「?」が表示されます。「£」の代わりに。

XMLで「£」を取得するにはどうすればよいですか? または、価格のデータを CDATA として渡すにはどうすればよいですか?

4

1 に答える 1

0

あなたの問題は、XML がファイルに書き込まれる方法に関係しているに違いありません。

これまでにいただいた情報を使用するプログラムを作成しました。XML 文字列を出力すると、プログラムは正しいものです。

データが XML ファイルに書き込まれるとき、または XML ファイルから読み戻されるときに、エラーが発生していると結論付けています。

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            new Program().Run();
        }

        void Run()
        {
            Class1 test = new Class1();
            test.Product = "Product";
            test.Price = "£100";

            Test(test);
        }

        void Test<T>(T obj)
        {
            XmlSerializerNamespaces Xsn = new XmlSerializerNamespaces();
            Xsn.Add("", ""); 
            XmlSerializer submit = new XmlSerializer(typeof(T)); 
            StringWriter stringWriter = new StringWriter(); 
            XmlWriter writer = XmlWriter.Create(stringWriter); 
            submit.Serialize(writer, obj, Xsn); 
            var xml = stringWriter.ToString(); // Your xml This is the serialization code. In this Obj is the object to serialize

            Console.WriteLine(xml);  // £ sign is fine in this output.
        }
    }

    [XmlRoot("Class1")]
    public class Class1
    {
        [XmlElement("Product")]
        public string Product
        {
            get;
            set;
        }

        [XmlElement("Price")]
        public string Price
        {
            get;
            set;
        }
    }

}
于 2013-05-15T12:26:58.317 に答える