-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialize.cs
228 lines (206 loc) · 6.95 KB
/
Serialize.cs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace CSharpHelper
{
#region 序列化
public class Serialize
{
/// <summary>
/// 序列化为对象
/// </summary>
/// <param name="objname"></param>
/// <param name="obj"></param>
public static void BinarySerialize(string objname,object obj)
{
try
{
string filename = objname + ".Binary";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
using (FileStream fileStream = new FileStream(filename, FileMode.Create))
{
// 用二进制格式序列化
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, obj);
fileStream.Close();
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 从二进制文件中反序列化
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static object BinaryDeserialize(string objname)
{
System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
//二进制格式反序列化
object obj;
string filename = objname + ".Binary";
if(!System.IO.File.Exists(filename))
throw new Exception("在反序列化之前,请先序列化");
using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
obj = formatter.Deserialize(stream);
stream.Close();
}
//using (FileStream fs = new FileStream(filename, FileMode.Open))
//{
// BinaryFormatter formatter = new BinaryFormatter();
// object obj = formatter.Deserialize(fs);
//}
return obj;
}
/// <summary>
/// 序列化为soap 即xml
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static void SoapSerialize(string objname,object obj)
{
try
{
string filename=objname+".Soap";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
using (FileStream fileStream = new FileStream(filename, FileMode.Create))
{
// 序列化为Soap
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fileStream, obj);
fileStream.Close();
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 反序列对象
/// </summary>
/// <param name="objname"></param>
public static object SoapDeserialize(string objname)
{
object obj;
System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
string filename=objname+".Soap";
if (!System.IO.File.Exists(filename))
throw new Exception("对反序列化之前,请先序列化");
//Soap格式反序列化
using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
obj = formatter.Deserialize(stream);
stream.Close();
}
return obj;
}
public static void XmlSerialize(string objname,object obj)
{
try
{
string filename=objname+".xml";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
using (FileStream fileStream = new FileStream(filename, FileMode.Create))
{
// 序列化为xml
XmlSerializer formatter = new XmlSerializer(typeof(Car));
formatter.Serialize(fileStream, obj);
fileStream.Close();
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 从xml序列中反序列化
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static object XmlDeserailize(string objname)
{
// System.Runtime.Serialization.IFormatter formatter = new XmlSerializer(typeof(Car));
string filename=objname+".xml";
object obj;
if (!System.IO.File.Exists(filename))
throw new Exception("对反序列化之前,请先序列化");
//Xml格式反序列化
using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
XmlSerializer formatter = new XmlSerializer(typeof(Car));
obj = (Car)formatter.Deserialize(stream);
stream.Close();
}
return obj;
}
}
#endregion
#region 要序列化的类
[Serializable]
public class Car
{
private string _Price;
private string _Owner;
private string m_filename;
[XmlElement(ElementName = "Price")]
public string Price
{
get { return this._Price; }
set { this._Price = value; }
}
[XmlElement(ElementName = "Owner")]
public string Owner
{
get { return this._Owner; }
set { this._Owner = value; }
}
public string Filename
{
get
{
return m_filename;
}
set
{
m_filename = value;
}
}
public Car(string o, string p)
{
this.Price = p;
this.Owner = o;
}
public Car()
{
}
}
#endregion
#region 调用示例
public class Demo
{
public void DemoFunction()
{
//序列化
Car car = new Car("chenlin", "120万");
Serialize.BinarySerialize("Binary序列化", car);
Serialize.SoapSerialize("Soap序列化", car);
Serialize.XmlSerialize("XML序列化", car);
//反序列化
Car car2 = (Car)Serialize.BinaryDeserialize("Binary序列化");
car2 = (Car)Serialize.SoapDeserialize("Soap序列化");
car2 = (Car)Serialize.XmlDeserailize("XML序列化");
}
}
#endregion
}