.Net框架中供给了3中序列化的体式格式, 分别是:
- Binary Serializer
- Soap Serializer
- XML Serializer
1. Binary Serializer
若是须要序列化这个类的对象,那么这个类必必要标识表记标帜为是可序列化的。须要用到特点 [Serializable] (感化域是Class, Struct, Enum, Delegate)
若是某些变量不须要序列化, 或者不克不及序列化可以应用[NonSerialized], (感化域是Field)
须要重视, 若是类中应用了其它的类属性,若是欲望它们能序列化,那么其它的类也必须是可序列化的, 如下面类DethDisease
1 namespace SerializationDemo
2 {
3 [Serializable]
4 public class Person
5 {
6 [NonSerialized]
7 private int _age;
8 public int Age
9 {
10 get
11 {
12 return _age;
13 }
14 set
15 {
16 _age = value;
17 }
18 }
19 public DethDisease Disease
20 {
21 get;
22 set;
23 }
24 public string Medicine
25 {
26 get;
27 set;
28 }
29 public Person()
30 {
31 Age = 0;
32 }
33 public bool Live()
34 {
35 Age++;
36 if (Disease != null && !Disease.CanLive(Age, Medicine))
37 return false;
38 return true;
39 }
40 }
41 }
2 {
3 [Serializable]
4 public class Person
5 {
6 [NonSerialized]
7 private int _age;
8 public int Age
9 {
10 get
11 {
12 return _age;
13 }
14 set
15 {
16 _age = value;
17 }
18 }
19 public DethDisease Disease
20 {
21 get;
22 set;
23 }
24 public string Medicine
25 {
26 get;
27 set;
28 }
29 public Person()
30 {
31 Age = 0;
32 }
33 public bool Live()
34 {
35 Age++;
36 if (Disease != null && !Disease.CanLive(Age, Medicine))
37 return false;
38 return true;
39 }
40 }
41 }
二进制序列化的代码, 主如果这个BinaryFormatter.
1 IFormatter formatter = new BinaryFormatter();
2 Stream stream = new FileStream("Person", FileMode.Create, FileAccess.Write, FileShare.None);
3 formatter.Serialize(stream, newPerson);
4 stream.Close();
2 Stream stream = new FileStream("Person", FileMode.Create, FileAccess.Write, FileShare.None);
3 formatter.Serialize(stream, newPerson);
4 stream.Close();
反序列化
1 IFormatter formatter = new BinaryFormatter();
2 Stream stream = new FileStream("Person", FileMode.Open, FileAccess.Read, FileShare.Read);
3 newPerson = (Person)formatter.Deserialize(stream);
4 stream.Close();
2 Stream stream = new FileStream("Person", FileMode.Open, FileAccess.Read, FileShare.Read);
3 newPerson = (Person)formatter.Deserialize(stream);
4 stream.Close();
2. Soap Serializer
根蒂根基和上方的一样,只是用到不合的IFormatter实现,SoapFormatter
3. XML Serializer
应用xml Serializer, 不须要应用特点[Serializable]
应用xml Serializer, 有相干的一些其它特点来改变xml的输出成果。
XmlRoot: 感化于类根元素, 把握根元素的输出
XmlElement: 感化于类的属性或field
XmlAttribute: 作为属性输出,而不是节点
XmlIgnore: 不输出
XmlText: 必须是字符串类型, 一个类中只能有一个这个特点, 没有标签包裹,也不作为属性
XmlArray: 感化于数组类型, 可以把握数组的输出
这里是常用的序列化xml的核心办法
1 public static string XmlSerialize<T>(T obj)
2 {
3 string xmlString = string.Empty;
4 XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
5 using (MemoryStream ms = new MemoryStream())
6 {
7 xmlSerializer.Serialize(ms, obj);
8 xmlString = Encoding.UTF8.GetString(ms.ToArray());
9 }
10 return xmlString;
11 }
12 public static T XmlDeserialize<T>(string xmlString)
13 {
14 T t = default(T);
15 XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
16 using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
17 {
18 using (XmlReader xmlReader = XmlReader.Create(xmlStream))
19 {
20 Object obj = xmlSerializer.Deserialize(xmlReader);
21 t = (T)obj;
22 }
23 }
24 return t;
25 }
2 {
3 string xmlString = string.Empty;
4 XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
5 using (MemoryStream ms = new MemoryStream())
6 {
7 xmlSerializer.Serialize(ms, obj);
8 xmlString = Encoding.UTF8.GetString(ms.ToArray());
9 }
10 return xmlString;
11 }
12 public static T XmlDeserialize<T>(string xmlString)
13 {
14 T t = default(T);
15 XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
16 using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
17 {
18 using (XmlReader xmlReader = XmlReader.Create(xmlStream))
19 {
20 Object obj = xmlSerializer.Deserialize(xmlReader);
21 t = (T)obj;
22 }
23 }
24 return t;
25 }
三种体式格式斗劲:
Binary体式格式效力最高,可读性不如后两种好。
Soap体式格式可以用于跨平台传输, xml体式格式简单易用,用于法度内部应用,灵活便利。
相干源代码: SerializationDemo