C# 序列化和反序列化(Xml,二进制)

序列化:将对象保存为文本文件或二进制文件;

反序列:将文本文件或二进制文件还原为对象;

1.Xml文件

1.1.创建StudentInfo类

 1 using System.Collections.Generic;
 2 using System.Xml.Serialization;
 3 using System;
 4 [Serializable]
 5 [XmlRoot("Student")]
 6 public class StudentInfo
 7 {
 8     [XmlElement("Name")]
 9     public string name;
10     [XmlElement("ID")]
11     public int id;
12     [XmlElement("Gender")]
13     public string gender;
14     [XmlArray("Hobby")]
15     public List<HobbyInfo> hobby;
16     public StudentInfo() {}
17     public StudentInfo(string _name,int _id,string _gender,List<HobbyInfo> _hobby)
18     {
19         this.name = _name;
20         this.id = _id;
21         this.gender = _gender;
22         this.hobby = _hobby;
23     }
24 }

1.2.创建HobbyInfo类

 1 using System;
 2 using System.Xml.Serialization;
 3 [Serializable]
 4 public class HobbyInfo
 5 {
 6     [XmlAttribute("Name")]
 7     public string name;
 8     [XmlAttribute("Info")]
 9     public string info;
10     public HobbyInfo() { }
11     public HobbyInfo(string _name,string _info)
12     {
13         this.name = _name;
14         this.info = _info;
15     }
16 }

1.3.序列化(生成Xml文件)

 1    void Start()
 2     {
 3         InitData();
 4         //StudentInfo info= XMLDeSerialize();
 5         //Debug.Log(info.name);
 6     }
 7     /// <summary>
 8     /// 初始化数据
 9     /// </summary>
10     void InitData()
11     {
12         HobbyInfo info1 = new HobbyInfo("篮球", "篮球info");
13         HobbyInfo info2 = new HobbyInfo("读书", "读书info");
14         List<HobbyInfo> info = new List<HobbyInfo>();
15         info.Add(info1);
16         info.Add(info2);
17         StudentInfo student = new StudentInfo("张三",1,"", info);
18         XMLSerialize(student);
19     }
20     /// <summary>
21     /// xml序列化
22     /// </summary>
23     void XMLSerialize(StudentInfo student)
24     {
25         string path = Application.dataPath + "/student.xml";
26         using (FileStream fs=new FileStream(path,FileMode.Create,FileAccess.ReadWrite))
27         {
28             XmlSerializer xml = new XmlSerializer(student.GetType());
29             xml.Serialize(fs, student);
30         }
31     }

生成的Xml如下图

1.4.反序列化

 1     /// <summary>
 2     /// xml反序列化
 3     /// </summary>
 4     /// <returns></returns>
 5     StudentInfo XMLDeSerialize()
 6     {
 7         StudentInfo student = new StudentInfo();
 8         string path = Application.dataPath + "/student.xml";
 9         using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
10         {
11             XmlSerializer xml = new XmlSerializer(typeof(StudentInfo));
12             student=(StudentInfo)xml.Deserialize(fs);
13 
14         }
15         return student;
16     }

2.二进制文件

2.1序列化(生成二进制文件)

1     void BinarySerialize(StudentInfo student)
2     {
3         string path = Application.dataPath + "/student.bytes";
4         using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
5         {
6             BinaryFormatter bf = new BinaryFormatter();
7             bf.Serialize(fs, student);
8         }
9     }

2.2反序列化

 1     StudentInfo BinaryDeSerialize()
 2     {
 3         StudentInfo student=new StudentInfo();
 4         string path = Application.dataPath + "/student.bytes";
 5         using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
 6         {
 7             BinaryFormatter bf = new BinaryFormatter();
 8             student = (StudentInfo)bf.Deserialize(fs);
 9         }
10         return student;
11     }
原文地址:https://www.cnblogs.com/mariolz/p/13742159.html