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

时间:2019-07-15 10:41:47

标签: c# xml-serialization

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

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

这是我的课程

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;
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以使用Newtonsoft.Json库解决此问题。要进行序列化,首先将对象转换为json,然后使用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

答案 1 :(得分:1)

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();
                }
            }