从discuz 里扒过来的一个通用序列化和反序列化类【转载】
感觉蛮好用的。我将一些常用的实体类型序列化成xml格式保存成xml,在系统运行时,先将其读取到缓存。
001 |
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.Text; |
004 |
using System.Xml.Serialization; |
005 |
using System.IO; |
006 |
using System.Xml; |
007 |
|
008 |
namespace HMYY.Common |
009 |
{ |
010 |
public class SerializationHelper |
011 |
{ |
012 |
|
013 |
/* 字典,用来存储XmlSerializer |
014 |
|
015 |
* 注意是此处是static |
016 |
|
017 |
* 实际上起缓存的作用 |
018 |
|
019 |
*/ |
020 |
private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>(); |
021 |
|
022 |
/// <summary> |
023 |
|
024 |
/// 为给予的类匹配一个XmlSerializer 对象, |
025 |
|
026 |
/// 若之前就使用过,则从缓存里调出来,若没有, |
027 |
|
028 |
/// 则新建一个对应的XmlSerializer 对象。 |
029 |
|
030 |
/// </summary> |
031 |
|
032 |
/// <param name="t">给予的类</param> |
033 |
|
034 |
/// <returns></returns> |
035 |
public static XmlSerializer GetSerializer(Type t) |
036 |
{ |
037 |
//先获得type 的哈希值,作为字典的索引 |
038 |
int type_hash = t.GetHashCode(); |
039 |
//如果字典里没有这个type的哈希,就创建一个,放到字典表里 |
040 |
|
041 |
if (!serializer_dict.ContainsKey(type_hash)) |
042 |
|
043 |
serializer_dict.Add(type_hash, new XmlSerializer(t)); |
044 |
|
045 |
//查询到这个哈希对应的对象,传出去 |
046 |
return serializer_dict[type_hash]; |
047 |
} |
048 |
|
049 |
/// <summary> |
050 |
/// 反序列化 |
051 |
/// </summary> |
052 |
/// <param name="type">对象类型</param> |
053 |
/// <param name="filepath">文件路径</param> |
054 |
/// <returns></returns> |
055 |
public static object Load(Type type, string filepath) |
056 |
{ |
057 |
FileStream fs = null; |
058 |
try |
059 |
{ |
060 |
fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read); |
061 |
XmlSerializer serializer = new XmlSerializer(type); |
062 |
return serializer.Deserialize(fs); |
063 |
} |
064 |
catch(Exception ex) |
065 |
{ |
066 |
throw ex; |
067 |
} |
068 |
finally |
069 |
{ |
070 |
if (fs != null) |
071 |
{ |
072 |
fs.Close(); |
073 |
fs.Dispose(); |
074 |
} |
075 |
} |
076 |
|
077 |
|
078 |
} |
079 |
|
080 |
/// <summary> |
081 |
/// 序列化 |
082 |
/// </summary> |
083 |
/// <param name="obj">对象</param> |
084 |
/// <param name="filename">文件路径</param> |
085 |
public static bool Save(object obj, string filepath) |
086 |
{ |
087 |
bool Isok = false; |
088 |
FileStream fs = null; |
089 |
try |
090 |
{ |
091 |
fs = new FileStream(filepath, FileMode.Create, FileAccess.Write); |
092 |
XmlSerializer serializer = new XmlSerializer(obj.GetType()); |
093 |
serializer.Serialize(fs, obj); |
094 |
Isok = true; |
095 |
} |
096 |
catch(Exception ex) |
097 |
{ |
098 |
throw ex; |
099 |
} |
100 |
finally |
101 |
{ |
102 |
if (fs != null) |
103 |
{ |
104 |
fs.Close(); |
105 |
fs.Dispose(); |
106 |
} |
107 |
} |
108 |
|
109 |
return Isok; |
110 |
} |
111 |
|
112 |
|
113 |
/// <summary> |
114 |
|
115 |
/// xml序列化成字符串 |
116 |
|
117 |
/// </summary> |
118 |
|
119 |
/// <param name="obj">对象</param> |
120 |
|
121 |
/// <returns>xml字符串</returns> |
122 |
|
123 |
public static string Serialize(object obj) |
124 |
{ |
125 |
|
126 |
string returnStr = ""; |
127 |
|
128 |
XmlSerializer serializer = GetSerializer(obj.GetType()); |
129 |
|
130 |
// 实例化一个内存流 |
131 |
|
132 |
MemoryStream ms = new MemoryStream(); |
133 |
|
134 |
XmlTextWriter xtw = null; |
135 |
|
136 |
StreamReader sr = null; |
137 |
|
138 |
try |
139 |
{ |
140 |
|
141 |
//实例化一个xml的写对象,其中使用utf-8 编码,写入到刚实例化的内存流中去 |
142 |
|
143 |
xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8); |
144 |
|
145 |
//设置缩进方式 |
146 |
|
147 |
xtw.Formatting = System.Xml.Formatting.Indented; |
148 |
|
149 |
|
150 |
|
151 |
//序列化它 |
152 |
|
153 |
serializer.Serialize(xtw, obj); |
154 |
|
155 |
/* |
156 |
|
157 |
* 此时内存流已经指向写完的地方, |
158 |
|
159 |
* 要再次读取到这个内存里的内容, |
160 |
|
161 |
* 需要将其"指针"指向内存流的开始 |
162 |
|
163 |
* 下面一句就是做这个事情 |
164 |
|
165 |
* param1 针对param2 的偏移量, |
166 |
|
167 |
* param2 内存开始的地方 |
168 |
|
169 |
*/ |
170 |
|
171 |
ms.Seek(0, SeekOrigin.Begin); |
172 |
|
173 |
//实例化一个流阅读器,去读内存流 |
174 |
|
175 |
sr = new StreamReader(ms); |
176 |
|
177 |
returnStr = sr.ReadToEnd(); |
178 |
|
179 |
} |
180 |
|
181 |
catch (Exception ex) |
182 |
{ |
183 |
|
184 |
throw ex; |
185 |
|
186 |
} |
187 |
|
188 |
finally |
189 |
{ |
190 |
|
191 |
if (xtw != null) |
192 |
|
193 |
xtw.Close(); |
194 |
|
195 |
if (sr != null) |
196 |
|
197 |
sr.Close(); |
198 |
|
199 |
ms.Close(); |
200 |
|
201 |
} |
202 |
|
203 |
return returnStr; |
204 |
|
205 |
|
206 |
|
207 |
} |
208 |
|
209 |
|
210 |
|
211 |
/// <summary> |
212 |
|
213 |
/// 反序列化 ,通过一个string |
214 |
|
215 |
/// </summary> |
216 |
|
217 |
/// <param name="type">反序列化的类的类型</param> |
218 |
|
219 |
/// <param name="s">序列源</param> |
220 |
|
221 |
/// <returns></returns> |
222 |
|
223 |
public static object DeSerialize(Type type, string s) |
224 |
{ |
225 |
|
226 |
/* |
227 |
|
228 |
* 此处将字符串转化为byte数组 |
229 |
|
230 |
* 其中值得注意的是,使用UTF-8 编码 |
231 |
|
232 |
* 就可以回避因为是非ASCII码被解析成乱码了 |
233 |
|
234 |
*/ |
235 |
|
236 |
byte[] b = System.Text.Encoding.UTF8.GetBytes(s); |
237 |
|
238 |
try |
239 |
{ |
240 |
|
241 |
XmlSerializer serializer = GetSerializer(type); |
242 |
|
243 |
return serializer.Deserialize(new MemoryStream(b)); |
244 |
|
245 |
} |
246 |
|
247 |
catch (Exception ex) |
248 |
{ |
249 |
|
250 |
throw ex; |
251 |
|
252 |
} |
253 |
|
254 |
} |
255 |
|
256 |
|
257 |
|
258 |
|
259 |
} |
260 |
} |

浙公网安备 33010602011771号