-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializeHelper.cs
203 lines (192 loc) · 6.32 KB
/
SerializeHelper.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
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
//using System.Runtime.Serialization.Json;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
namespace CSharpHelper
{
public class SerializeHelper
{
public SerializeHelper()
{ }
#region XML序列化
/// <summary>
/// 文件化XML序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static void Save(object obj, string filename)
{
FileStream fs = null;
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null) fs.Close();
}
}
/// <summary>
/// 文件化XML反序列化
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="filename">文件路径</param>
public static object Load(Type type, string filename)
{
FileStream fs = null;
try
{
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null) fs.Close();
}
}
/// <summary>
/// 文本化XML序列化
/// </summary>
/// <param name="item">对象</param>
public string ToXml<T>(T item)
{
XmlSerializer serializer = new XmlSerializer(item.GetType());
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
serializer.Serialize(writer, item);
return sb.ToString();
}
}
/// <summary>
/// 文本化XML反序列化
/// </summary>
/// <param name="str">字符串序列</param>
public T FromXml<T>(string str)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (XmlReader reader = new XmlTextReader(new StringReader(str)))
{
return (T)serializer.Deserialize(reader);
}
}
#endregion
#region Json序列化
/// <summary>
/// JsonSerializer序列化
/// </summary>
/// <param name="item">对象</param>
//public string ToJson<T>(T item)
//{
// DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());
// using (MemoryStream ms = new MemoryStream())
// {
// serializer.WriteObject(ms, item);
// return Encoding.UTF8.GetString(ms.ToArray());
// }
//}
///// <summary>
///// JsonSerializer反序列化
///// </summary>
///// <param name="str">字符串序列</param>
//public T FromJson<T>(string str) where T : class
//{
// DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
// using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
// {
// return serializer.ReadObject(ms) as T;
// }
//}
#endregion
#region SoapFormatter序列化
/// <summary>
/// SoapFormatter序列化
/// </summary>
/// <param name="item">对象</param>
public string ToSoap<T>(T item)
{
SoapFormatter formatter = new SoapFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, item);
ms.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);
return xmlDoc.InnerXml;
}
}
/// <summary>
/// SoapFormatter反序列化
/// </summary>
/// <param name="str">字符串序列</param>
public T FromSoap<T>(string str)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(str);
SoapFormatter formatter = new SoapFormatter();
using (MemoryStream ms = new MemoryStream())
{
xmlDoc.Save(ms);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
#endregion
#region BinaryFormatter序列化
/// <summary>
/// BinaryFormatter序列化
/// </summary>
/// <param name="item">对象</param>
public string ToBinary<T>(T item)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, item);
ms.Position = 0;
byte[] bytes = ms.ToArray();
StringBuilder sb = new StringBuilder();
foreach (byte bt in bytes)
{
sb.Append(string.Format("{0:X2}", bt));
}
return sb.ToString();
}
}
/// <summary>
/// BinaryFormatter反序列化
/// </summary>
/// <param name="str">字符串序列</param>
public T FromBinary<T>(string str)
{
int intLen = str.Length / 2;
byte[] bytes = new byte[intLen];
for (int i = 0; i < intLen; i++)
{
int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16);
bytes[i] = (byte)ibyte;
}
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(bytes))
{
return (T)formatter.Deserialize(ms);
}
}
#endregion
}
}