.net下有一种技术叫做对象序列化,说得通俗一点,C#序列化就是把一个对象保存到一个文件或数据库字段中去,C#反序列化就是在需要的时候再把这个文件转化成原来的对象使用。
在.NET中常见的C#序列化的方法主要也有三个:二进制序列化、XML序列化、SOAP序列化。
下面通过一个小例子来说明这三种方法的使用。
复制
using System; using System.Collections.Generic; using System.Text; namespace FileSerializer { [Serializable] public class Book { string id; string name; public string Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public Book() { } public Book(string id,string name) { this.id = id; this.name = name; } public override string ToString() { return "编号:" + id + "\t名称:" + name; } } } using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml.Serialization; namespace FileSerializer { public abstract class Serializer< T> { string filePath; public string FilePath { get { return filePath; } set { filePath = value; } } public Serializer(string filePath) { this.filePath = filePath; } public void Serialize(T serializeObj) { using (Stream st = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { S(st, serializeObj); } } protected abstract void S(Stream st, T serializeObj); public T Deserialize() { using (Stream st = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { return D(st); } } protected abstract T D(Stream st); } } using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace FileSerializer { class SerializerBinary< T> : Serializer< T> { public SerializerBinary(string filePath) : base(filePath) { } protected override void S(Stream st, T serializeObj) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(st, serializeObj); } protected override T D(Stream st) { BinaryFormatter bf = new BinaryFormatter(); return (T)bf.Deserialize(st); } } } using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Soap; namespace FileSerializer { public class SerializerSoap< T> : Serializer< T> { public SerializerSoap(string filePath) : base(filePath) { } protected override void S(Stream st, T serializeObj) { SoapFormatter sf = new SoapFormatter(); sf.Serialize(st, serializeObj); } protected override T D(Stream st) { SoapFormatter sf = new SoapFormatter(); return (T)sf.Deserialize(st); } } } using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization; using System.IO; namespace FileSerializer { public class SerializerXml< T> : Serializer< T> { public SerializerXml(string filePath) : base(filePath) { } protected override void S(Stream st, T serializeObj) { XmlSerializer xs = new XmlSerializer(typeof(T)); xs.Serialize(st, serializeObj); } protected override T D(Stream st) { XmlSerializer xs = new XmlSerializer(typeof(T)); return (T)xs.Deserialize(st); } } } using System; using System.Collections.Generic; using System.Text; namespace FileSerializer { class Program { static void Main(string[] args) { Book book = new Book("01","C#程序设计入门01"); Serializer< Book> serializer = new SerializerBinary< Book>("bookBinary"); serializer.Serialize(book); Book newbook = serializer.Deserialize(); Console.WriteLine(newbook.ToString()); book = new Book("02", "C#程序设计入门02"); serializer = new SerializerSoap< Book>("bookSoap.soap"); serializer.Serialize(book); newbook = serializer.Deserialize(); Console.WriteLine(newbook.ToString()); book = new Book("03", "C#程序设计入门03"); serializer = new SerializerXml< Book>("bookXml.xml"); serializer.Serialize(book); newbook = serializer.Deserialize(); Console.WriteLine(newbook.ToString()); Console.ReadLine(); } } }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
C#序列化和反序列化的例子就和大家讨论到这里。
【编辑推荐】