SerializationHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. namespace CB.Common
  8. {
  9. /// <summary>
  10. /// 序列化
  11. /// </summary>
  12. public class SerializationHelper
  13. {
  14. private SerializationHelper()
  15. { }
  16. private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
  17. public static XmlSerializer GetSerializer(Type t)
  18. {
  19. int type_hash = t.GetHashCode();
  20. if (!serializer_dict.ContainsKey(type_hash))
  21. serializer_dict.Add(type_hash, new XmlSerializer(t));
  22. return serializer_dict[type_hash];
  23. }
  24. /// <summary>
  25. /// 反序列化
  26. /// </summary>
  27. /// <param name="type">对象类型</param>
  28. /// <param name="filename">文件路径</param>
  29. /// <returns></returns>
  30. public static object Load(Type type, string filename)
  31. {
  32. FileStream fs = null;
  33. try
  34. {
  35. fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  36. XmlSerializer serializer = new XmlSerializer(type);
  37. return serializer.Deserialize(fs);
  38. }
  39. catch (Exception ex)
  40. {
  41. throw ex;
  42. }
  43. finally
  44. {
  45. if (fs != null)
  46. fs.Close();
  47. }
  48. }
  49. /// <summary>
  50. /// 序列化
  51. /// </summary>
  52. /// <param name="obj">对象</param>
  53. /// <param name="filename">文件路径</param>
  54. public static bool Save(object obj, string filename)
  55. {
  56. bool success = false;
  57. FileStream fs = null;
  58. // serialize it...
  59. try
  60. {
  61. fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  62. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  63. serializer.Serialize(fs, obj);
  64. success = true;
  65. }
  66. catch (Exception ex)
  67. {
  68. throw ex;
  69. }
  70. finally
  71. {
  72. if (fs != null)
  73. fs.Close();
  74. }
  75. return success;
  76. }
  77. /// <summary>
  78. /// xml序列化成字符串
  79. /// </summary>
  80. /// <param name="obj">对象</param>
  81. /// <returns>xml字符串</returns>
  82. public static string Serialize(object obj)
  83. {
  84. string returnStr = "";
  85. XmlSerializer serializer = GetSerializer(obj.GetType());
  86. MemoryStream ms = new MemoryStream();
  87. XmlTextWriter xtw = null;
  88. StreamReader sr = null;
  89. try
  90. {
  91. xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
  92. xtw.Formatting = System.Xml.Formatting.Indented;
  93. serializer.Serialize(xtw, obj);
  94. ms.Seek(0, SeekOrigin.Begin);
  95. sr = new StreamReader(ms);
  96. returnStr = sr.ReadToEnd();
  97. }
  98. catch (Exception ex)
  99. {
  100. throw ex;
  101. }
  102. finally
  103. {
  104. if (xtw != null)
  105. xtw.Close();
  106. if (sr != null)
  107. sr.Close();
  108. ms.Close();
  109. }
  110. return returnStr;
  111. }
  112. public static object DeSerialize(Type type, string s)
  113. {
  114. byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
  115. try
  116. {
  117. XmlSerializer serializer = GetSerializer(type);
  118. return serializer.Deserialize(new MemoryStream(b));
  119. }
  120. catch (Exception ex)
  121. {
  122. throw ex;
  123. }
  124. }
  125. }
  126. }