C#中的递归序列化 [英] Recursive Serialization in C#

查看:56
本文介绍了C#中的递归序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将myFamily序列化为一个.xml文件,我真的不知道该怎么做.

I need to serialize myFamily to an .xml file, I really don't know how to do it.

Enums.cs

public enum Genre {
    Male,
    Female
}

PERSON.cs

public class PERSON {
    public string Name { get; set; }
    public Genre Genre { get; set; }
    public List<PERSON> Parents { get; set; }
    public List<PERSON> Children { get; set; }

    public PERSON(string name, Genre genre) {
        this.Name = name;
        this.Genre = genre;
    }
}

Form1.cs

    private void Form1_Load(object sender, EventArgs e) {
        List<PERSON> myFamily = new List<PERSON>();

        PERSON Andrew = new PERSON("Andrew", Genre.Male);
        PERSON Angela = new PERSON("Angela", Genre.Female);
        PERSON Tina = new PERSON("Tina", Genre.Female);
        PERSON Jason = new PERSON("Jason", Genre.Male);
        PERSON Amanda = new PERSON("Amanda", Genre.Female);
        PERSON Steven = new PERSON("Steven", Genre.Male);

        Andrew.Parents.Add(Tina);
        Andrew.Parents.Add(Jason);

        Angela.Parents.Add(Tina);
        Angela.Parents.Add(Jason);

        Tina.Parents.Add(Amanda);
        Tina.Parents.Add(Steven);

        Jason.Children.Add(Andrew);
        Jason.Children.Add(Angela);

        Tina.Children.Add(Andrew);
        Tina.Children.Add(Angela);

        Amanda.Children.Add(Tina);

        Steven.Children.Add(Tina);

        myFamily.Add(Andrew);
        myFamily.Add(Angela);
        myFamily.Add(Tina);
        myFamily.Add(Jason);
        myFamily.Add(Amanda);
        myFamily.Add(Steven);

        // serialize to an .xml file
    }

推荐答案

要使用循环引用序列化对象,您需要使用 DataContractSerializer.这样做

To Serialize objects with circular reference you need to use DataContractSerializer. To do so

  • [DataContract(IsReference=true)]添加到Person
  • [DataMember] 添加到您的属性
  • 在构造函数中实例化你的列表
  • 记得使用System.Runtime.Serialization;
  • Add [DataContract(IsReference=true)] to Person class
  • Add [DataMember] to your properties
  • Instantiate your lists in constructor
  • Remember to using System.Runtime.Serialization;

所以你的类应该是:

[DataContract(IsReference=true)]
public class PERSON
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public Genre Genre { get; set; }
    [DataMember]
    public List<PERSON> Parents { get; set; }
    [DataMember]
    public List<PERSON> Children { get; set; }

    public PERSON(string name, Genre genre)
    {
        this.Name = name;
        this.Genre = genre;
        Parents = new List<PERSON>();
        Children = new List<PERSON>();
    }
}

序列化:

var serializer = new DataContractSerializer(myFamily.GetType()); 
using (FileStream stream = File.Create(@"D:\Test.Xml")) 
{ 
    serializer.WriteObject(stream, myFamily); 
} 

反序列化:

using (FileStream stream = File.OpenRead(@"D:\Test.Xml"))
{ 
    List<PERSON> data = (List<PERSON>)serializer.ReadObject(stream); 
}

这篇关于C#中的递归序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆