SerializeHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. //using System.Runtime.Serialization.Json;
  7. using System.Runtime.Serialization.Formatters.Soap;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. namespace Ant.Service.Utilities
  10. {
  11. public class SerializeHelper
  12. {
  13. public SerializeHelper()
  14. { }
  15. #region XML序列化
  16. /// <summary>
  17. /// 文件化XML序列化
  18. /// </summary>
  19. /// <param name="obj">对象</param>
  20. /// <param name="filename">文件路径</param>
  21. public static void Save(object obj, string filename)
  22. {
  23. FileStream fs = null;
  24. try
  25. {
  26. fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  27. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  28. serializer.Serialize(fs, obj);
  29. }
  30. catch (Exception ex)
  31. {
  32. throw ex;
  33. }
  34. finally
  35. {
  36. if (fs != null) fs.Close();
  37. }
  38. }
  39. /// <summary>
  40. /// 文件化XML反序列化
  41. /// </summary>
  42. /// <param name="type">对象类型</param>
  43. /// <param name="filename">文件路径</param>
  44. public static object Load(Type type, string filename)
  45. {
  46. FileStream fs = null;
  47. try
  48. {
  49. fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  50. XmlSerializer serializer = new XmlSerializer(type);
  51. return serializer.Deserialize(fs);
  52. }
  53. catch (Exception ex)
  54. {
  55. throw ex;
  56. }
  57. finally
  58. {
  59. if (fs != null) fs.Close();
  60. }
  61. }
  62. /// <summary>
  63. /// 文本化XML序列化
  64. /// </summary>
  65. /// <param name="item">对象</param>
  66. public string ToXml<T>(T item)
  67. {
  68. XmlSerializer serializer = new XmlSerializer(item.GetType());
  69. StringBuilder sb = new StringBuilder();
  70. using (XmlWriter writer = XmlWriter.Create(sb))
  71. {
  72. serializer.Serialize(writer, item);
  73. return sb.ToString();
  74. }
  75. }
  76. /// <summary>
  77. /// 文本化XML反序列化
  78. /// </summary>
  79. /// <param name="str">字符串序列</param>
  80. public T FromXml<T>(string str)
  81. {
  82. XmlSerializer serializer = new XmlSerializer(typeof(T));
  83. using (XmlReader reader = new XmlTextReader(new StringReader(str)))
  84. {
  85. return (T)serializer.Deserialize(reader);
  86. }
  87. }
  88. #endregion
  89. #region Json序列化
  90. /// <summary>
  91. /// JsonSerializer序列化
  92. /// </summary>
  93. /// <param name="item">对象</param>
  94. //public string ToJson<T>(T item)
  95. //{
  96. // DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());
  97. // using (MemoryStream ms = new MemoryStream())
  98. // {
  99. // serializer.WriteObject(ms, item);
  100. // return Encoding.UTF8.GetString(ms.ToArray());
  101. // }
  102. //}
  103. ///// <summary>
  104. ///// JsonSerializer反序列化
  105. ///// </summary>
  106. ///// <param name="str">字符串序列</param>
  107. //public T FromJson<T>(string str) where T : class
  108. //{
  109. // DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
  110. // using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
  111. // {
  112. // return serializer.ReadObject(ms) as T;
  113. // }
  114. //}
  115. #endregion
  116. #region SoapFormatter序列化
  117. /// <summary>
  118. /// SoapFormatter序列化
  119. /// </summary>
  120. /// <param name="item">对象</param>
  121. public string ToSoap<T>(T item)
  122. {
  123. SoapFormatter formatter = new SoapFormatter();
  124. using (MemoryStream ms = new MemoryStream())
  125. {
  126. formatter.Serialize(ms, item);
  127. ms.Position = 0;
  128. XmlDocument xmlDoc = new XmlDocument();
  129. xmlDoc.Load(ms);
  130. return xmlDoc.InnerXml;
  131. }
  132. }
  133. /// <summary>
  134. /// SoapFormatter反序列化
  135. /// </summary>
  136. /// <param name="str">字符串序列</param>
  137. public T FromSoap<T>(string str)
  138. {
  139. XmlDocument xmlDoc = new XmlDocument();
  140. xmlDoc.LoadXml(str);
  141. SoapFormatter formatter = new SoapFormatter();
  142. using (MemoryStream ms = new MemoryStream())
  143. {
  144. xmlDoc.Save(ms);
  145. ms.Position = 0;
  146. return (T)formatter.Deserialize(ms);
  147. }
  148. }
  149. #endregion
  150. #region BinaryFormatter序列化
  151. /// <summary>
  152. /// BinaryFormatter序列化
  153. /// </summary>
  154. /// <param name="item">对象</param>
  155. public string ToBinary<T>(T item)
  156. {
  157. BinaryFormatter formatter = new BinaryFormatter();
  158. using (MemoryStream ms = new MemoryStream())
  159. {
  160. formatter.Serialize(ms, item);
  161. ms.Position = 0;
  162. byte[] bytes = ms.ToArray();
  163. StringBuilder sb = new StringBuilder();
  164. foreach (byte bt in bytes)
  165. {
  166. sb.Append(string.Format("{0:X2}", bt));
  167. }
  168. return sb.ToString();
  169. }
  170. }
  171. /// <summary>
  172. /// BinaryFormatter反序列化
  173. /// </summary>
  174. /// <param name="str">字符串序列</param>
  175. public T FromBinary<T>(string str)
  176. {
  177. int intLen = str.Length / 2;
  178. byte[] bytes = new byte[intLen];
  179. for (int i = 0; i < intLen; i++)
  180. {
  181. int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16);
  182. bytes[i] = (byte)ibyte;
  183. }
  184. BinaryFormatter formatter = new BinaryFormatter();
  185. using (MemoryStream ms = new MemoryStream(bytes))
  186. {
  187. return (T)formatter.Deserialize(ms);
  188. }
  189. }
  190. #endregion
  191. }
  192. }