- A+
所属分类:.NET技术
序列化方式说明
-
二进制序列化器:序列化结果人看不懂,但是序列化后体积小
-
soap序列化器:人能看懂,没啥阅读体验,文件体积比较大
-
xml序列化器:可阅读性好,体积中等
-
序列化为json格式
二进制序列化器
- 要序列化的实体必须标记
[Serializable]
特性,表示可以序列化 - 不想序列化的字段可以标记
[NonSerialized]
特性
实体
[Serializable] public class People { public int Id; public string Name; public string Address; [NonSerialized] public string Sex; }
序列化
List<People> peoples = GetPeoples();//创建一个实体集合 string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", "BinarySerialize.txt"); using (FileStream fs = new FileStream(fileName,FileMode.OpenOrCreate, FileAccess.ReadWrite)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, peoples); }
反序列化
string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", "BinarySerialize.txt"); using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { BinaryFormatter formatter = new BinaryFormatter(); List<People> peoples = formatter.Deserialize(fs) as List<People>; }
序列化结果
一个看不懂的二进制文档
soap序列化器
- 要序列化的实体必须标记
[Serializable]
特性,表示可以序列化 - 不想序列化的字段可以标记
[NonSerialized]
特性 - soap不能序列化泛型对象
实体
[Serializable] public class People { public int Id; public string Name; public string Address; [NonSerialized] public string Sex; }
序列化
List<People> peoples = GetPeoples(); string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", "SoapSerialize.txt"); using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { SoapFormatter formatter = new SoapFormatter(); formatter.Serialize(fs, peoples.ToArray()); }
反序列化
string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", "SoapSerialize.txt"); using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { SoapFormatter formatter = new SoapFormatter(); People[] peoples = (People[])formatter.Deserialize(fs); }
序列化结果
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <SOAP-ENC:Array SOAP-ENC:arrayType="a1:People[3]" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/对象序列化/对象序列化%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <item href="#ref-3"/> <item href="#ref-4"/> <item href="#ref-5"/> </SOAP-ENC:Array> <a1:People id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/对象序列化/对象序列化%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <Id>1</Id> <Name id="ref-6">张三</Name> <Address id="ref-7">江苏</Address> </a1:People> <a1:People id="ref-4" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/对象序列化/对象序列化%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <Id>2</Id> <Name id="ref-8">李四</Name> <Address id="ref-9">安徽</Address> </a1:People> <a1:People id="ref-5" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/对象序列化/对象序列化%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <Id>3</Id> <Name id="ref-10">王五</Name> <Address id="ref-11">浙江</Address> </a1:People> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
XML序列化器
- 创建XML序列化器时需要传入需要序列化的对象的类型
- 不想序列化的字段可以标记
[XmlIgnore]
特性
实体
public class People { public int Id; [XmlElement(ElementName = "姓名")]//设置节点别名 public string Name; [XmlIgnore]//忽略Address不进行序列化 public string Address; [XmlAttribute(AttributeName = "性别")]//设置这个字段作为xml中的属性出现,并设置了属性名称 public string Sex; }
序列化
List<People> peoples = GetPeoples(); string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", "XmlSerialize.xml"); using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { XmlSerializer formatter = new XmlSerializer(typeof(List<People>)); formatter.Serialize(fs, peoples); }
反序列化
string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", "XmlSerialize.xml"); using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { XmlSerializer formatter = new XmlSerializer(typeof(List<People>)); List<People> peoples = (List<People>)formatter.Deserialize(fs); }
序列化结果
<?xml version="1.0"?> <ArrayOfPeople xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <People 性别="男"> <Id>1</Id> <姓名>张三</姓名> </People> <People 性别="女"> <Id>2</Id> <姓名>李四</姓名> </People> <People 性别="男"> <Id>3</Id> <姓名>王五</姓名> </People> </ArrayOfPeople>
序列化为Json格式
- 安装Nuget包
Newtonsoft.Json
序列化和反序列化
List<People> peoples = GetPeoples(); string json = JsonConvert.SerializeObject(peoples); List<People> newPeople = JsonConvert.DeserializeObject<List<People>>(json);
序列化结果
[{"Id":1,"Name":"张三","Address":"江苏"},{"Id":2,"Name":"李四","Address":"安徽"},{"Id":3,"Name":"王五","Address":"浙江"}]