Serialize.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Xml;
  3. using System.IO;
  4. using System.Xml.Serialization;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Runtime.Serialization.Formatters.Soap;
  7. namespace Ant.Service.Utilities
  8. {
  9. #region 调用示例
  10. public class Demo
  11. {
  12. public void DemoFunction()
  13. {
  14. //序列化
  15. Car car = new Car("chenlin", "120万");
  16. Serialize.BinarySerialize("Binary序列化", car);
  17. Serialize.SoapSerialize("Soap序列化", car);
  18. Serialize.XmlSerialize("XML序列化", car);
  19. //反序列化
  20. Car car2 = (Car)Serialize.BinaryDeserialize("Binary序列化");
  21. car2 = (Car)Serialize.SoapDeserialize("Soap序列化");
  22. car2 = (Car)Serialize.XmlDeserailize("XML序列化");
  23. }
  24. }
  25. #endregion
  26. #region 序列化
  27. public class Serialize
  28. {
  29. /// <summary>
  30. /// 序列化为对象
  31. /// </summary>
  32. /// <param name="objname"></param>
  33. /// <param name="obj"></param>
  34. public static void BinarySerialize(string objname, object obj)
  35. {
  36. try
  37. {
  38. string filename = objname + ".Binary";
  39. if (System.IO.File.Exists(filename))
  40. System.IO.File.Delete(filename);
  41. using (FileStream fileStream = new FileStream(filename, FileMode.Create))
  42. {
  43. // 用二进制格式序列化
  44. BinaryFormatter binaryFormatter = new BinaryFormatter();
  45. binaryFormatter.Serialize(fileStream, obj);
  46. fileStream.Close();
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. throw new Exception(ex.Message);
  52. }
  53. }
  54. /// <summary>
  55. /// 从二进制文件中反序列化
  56. /// </summary>
  57. /// <param name="objname"></param>
  58. /// <returns></returns>
  59. public static object BinaryDeserialize(string objname)
  60. {
  61. System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
  62. //二进制格式反序列化
  63. object obj;
  64. string filename = objname + ".Binary";
  65. if (!System.IO.File.Exists(filename))
  66. throw new Exception("在反序列化之前,请先序列化");
  67. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  68. {
  69. obj = formatter.Deserialize(stream);
  70. stream.Close();
  71. }
  72. //using (FileStream fs = new FileStream(filename, FileMode.Open))
  73. //{
  74. // BinaryFormatter formatter = new BinaryFormatter();
  75. // object obj = formatter.Deserialize(fs);
  76. //}
  77. return obj;
  78. }
  79. /// <summary>
  80. /// 序列化为soap 即xml
  81. /// </summary>
  82. /// <param name="objname"></param>
  83. /// <returns></returns>
  84. public static void SoapSerialize(string objname, object obj)
  85. {
  86. try
  87. {
  88. string filename = objname + ".Soap";
  89. if (System.IO.File.Exists(filename))
  90. System.IO.File.Delete(filename);
  91. using (FileStream fileStream = new FileStream(filename, FileMode.Create))
  92. {
  93. // 序列化为Soap
  94. SoapFormatter formatter = new SoapFormatter();
  95. formatter.Serialize(fileStream, obj);
  96. fileStream.Close();
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. throw new Exception(ex.Message);
  102. }
  103. }
  104. /// <summary>
  105. /// 反序列对象
  106. /// </summary>
  107. /// <param name="objname"></param>
  108. public static object SoapDeserialize(string objname)
  109. {
  110. object obj;
  111. System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
  112. string filename = objname + ".Soap";
  113. if (!System.IO.File.Exists(filename))
  114. throw new Exception("对反序列化之前,请先序列化");
  115. //Soap格式反序列化
  116. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  117. {
  118. obj = formatter.Deserialize(stream);
  119. stream.Close();
  120. }
  121. return obj;
  122. }
  123. public static void XmlSerialize(string objname, object obj)
  124. {
  125. try
  126. {
  127. string filename = objname + ".xml";
  128. if (System.IO.File.Exists(filename))
  129. System.IO.File.Delete(filename);
  130. using (FileStream fileStream = new FileStream(filename, FileMode.Create))
  131. {
  132. // 序列化为xml
  133. XmlSerializer formatter = new XmlSerializer(typeof(Car));
  134. formatter.Serialize(fileStream, obj);
  135. fileStream.Close();
  136. }
  137. }
  138. catch (Exception ex)
  139. {
  140. throw new Exception(ex.Message);
  141. }
  142. }
  143. /// <summary>
  144. /// 从xml序列中反序列化
  145. /// </summary>
  146. /// <param name="objname"></param>
  147. /// <returns></returns>
  148. public static object XmlDeserailize(string objname)
  149. {
  150. // System.Runtime.Serialization.IFormatter formatter = new XmlSerializer(typeof(Car));
  151. string filename = objname + ".xml";
  152. object obj;
  153. if (!System.IO.File.Exists(filename))
  154. throw new Exception("对反序列化之前,请先序列化");
  155. //Xml格式反序列化
  156. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  157. {
  158. XmlSerializer formatter = new XmlSerializer(typeof(Car));
  159. obj = (Car)formatter.Deserialize(stream);
  160. stream.Close();
  161. }
  162. return obj;
  163. }
  164. }
  165. #endregion
  166. #region 要序列化的类
  167. [Serializable]
  168. public class Car
  169. {
  170. private string _Price;
  171. private string _Owner;
  172. private string m_filename;
  173. [XmlElement(ElementName = "Price")]
  174. public string Price
  175. {
  176. get { return this._Price; }
  177. set { this._Price = value; }
  178. }
  179. [XmlElement(ElementName = "Owner")]
  180. public string Owner
  181. {
  182. get { return this._Owner; }
  183. set { this._Owner = value; }
  184. }
  185. public string Filename
  186. {
  187. get
  188. {
  189. return m_filename;
  190. }
  191. set
  192. {
  193. m_filename = value;
  194. }
  195. }
  196. public Car(string o, string p)
  197. {
  198. this.Price = p;
  199. this.Owner = o;
  200. }
  201. public Car()
  202. {
  203. }
  204. }
  205. #endregion
  206. }