如何反序列化具有字典作为成员的自定义对象?

How to Deserialize a custom object having dictionary as a member?

我需要反序列化具有 2 个字段作为字符串和一个字典的对象,我尝试转换但它抛出错误为“Cannot serialize member MVVMDemo.Model.Student.books of type System.Collections.Generic.Dictionary” 当没有字典项时它是可行的但是当我添加该字典项时它无法转换。从下面的行抛出错误

XmlSerializer serializer = new XmlSerializer(typeof(Student));

这是我的class

public class Student
    {
        private string firstName;
        private string lastName;
        public Dictionary<string, string> books;

        public Dictionary<string, string> Books
        {
            get{return books;}
            set{books = value;}
        }
        public string FirstName
        {
            get{return firstName;}
            set
            {
                if (firstName != value)
                {
                    firstName = value;
                }
            }
        }

        public string LastName
        {
            get { return lastName; }
            set
            {
                if (lastName != value)
                {
                    lastName = value;
                }
            }
        }
    }

你可以用Newtonsoft.Json library. To serialize start with converting your object to json, then use DeserializeXNode方法解决这个问题:

var student = new Student()
{
    FirstName = "FirstName",
    LastName = "LastName",
    Books = new Dictionary<string, string>
    {
        { "abc", "42" },
    }
};

var json = JsonConvert.SerializeObject(student);
var xml = JsonConvert.DeserializeXNode(json, "Root");
var result = xml.ToString(SaveOptions.None);
// result is "<Root><books><abc>42</abc></books><Books><abc>42</abc></Books><FirstName>FirstName</FirstName><LastName>LastName</LastName></Root>"

要反序列化,您可以使用 SerializeXmlNode 方法:

var input = "<Root><books><abc>42</abc></books><Books><abc>42</abc></Books><FirstName>FirstName</FirstName><LastName>LastName</LastName></Root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(input);
var json = JsonConvert.SerializeXmlNode(doc);
var template = new {Root = (Student) null};
var result = JsonConvert.DeserializeAnonymousType(json, template);
// result.Root is an instance of Student class

XmlSerializer 不支持 Dictionary 的原因是 Dictionary、HashTable 等类型需要一个相等比较器,不能轻易序列化为 XML。解决这个问题

使用DataContractSerizalizer

[DataContract]
public class Student
{
    private string firstName;
    private string lastName;

    private Dictionary<string, string> books = new Dictionary<string, string>();
    [DataMember]
    public Dictionary<string, string> Books
    {
        get => books;
        set => books = value;
    }
    [DataMember]
    public string FirstName
    {
        get { return firstName; }
        set
        {
            if (firstName != value)
            {
                firstName = value;
            }
        }
    }
    [DataMember]
    public string LastName
    {
        get { return lastName; }
        set
        {
            if (lastName != value)
            {
                lastName = value;
            }
        }
    }
}


var serializer = new DataContractSerializer(typeof(Student));
            using (var sw = new StringWriter()){
                using (var writer = new XmlTextWriter(sw))
                {
                    serializer.WriteObject(writer, student);
                    writer.Flush();
                    var xml = sw.ToString();
                }
            }