SerializationHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Xml.Serialization;
  6. using System.Data;
  7. using System.Reflection;
  8. using System.Collections;
  9. namespace Ant.Service.Common
  10. {
  11. /// <summary>
  12. /// 序列化反序列化对象帮助类
  13. /// add 作者: 季健国 QQ:181589805 by 2016-03-09
  14. /// </summary>
  15. public class SerializationHelper
  16. {
  17. public SerializationHelper() { }
  18. /// <summary>
  19. /// 反序列化
  20. /// </summary>
  21. /// <param name="type">对象类型</param>
  22. /// <param name="filename">文件路径</param>
  23. /// <returns></returns>
  24. public static object Load(Type type, string filename)
  25. {
  26. FileStream fs = null;
  27. try
  28. {
  29. // open the stream...
  30. fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  31. XmlSerializer serializer = new XmlSerializer(type);
  32. return serializer.Deserialize(fs);
  33. }
  34. catch (Exception ex)
  35. {
  36. throw ex;
  37. }
  38. finally
  39. {
  40. if (fs != null)
  41. fs.Close();
  42. }
  43. }
  44. /// <summary>
  45. /// 序列化
  46. /// </summary>
  47. /// <param name="obj">对象</param>
  48. /// <param name="filename">文件路径</param>
  49. public static void Save(object obj, string filename)
  50. {
  51. FileStream fs = null;
  52. // serialize it...
  53. try
  54. {
  55. fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  56. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  57. serializer.Serialize(fs, obj);
  58. }
  59. catch (Exception ex)
  60. {
  61. throw ex;
  62. }
  63. finally
  64. {
  65. if (fs != null)
  66. fs.Close();
  67. }
  68. }
  69. /// <summary>
  70. /// 序列化XML字符串
  71. /// </summary>
  72. /// <param name="obj">对象</param>
  73. public static string Save(object obj)
  74. {
  75. MemoryStream ms = new MemoryStream();
  76. System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(ms, Encoding.Default);
  77. // serialize it...
  78. try
  79. {
  80. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  81. serializer.Serialize(xtw, obj);
  82. return Encoding.Default.GetString(ms.ToArray());
  83. }
  84. catch (Exception ex)
  85. {
  86. throw ex;
  87. }
  88. finally
  89. {
  90. xtw.Close();
  91. ms.Dispose();
  92. }
  93. }
  94. /// <summary>
  95. /// 反序列化XML字符串
  96. /// </summary>
  97. /// <param name="type">类型</param>
  98. /// <param name="xml">XML字符串</param>
  99. /// <returns></returns>
  100. public static object Get(Type type, string xml)
  101. {
  102. MemoryStream ms = null;
  103. StreamReader sr = null;
  104. try
  105. {
  106. XmlSerializer serializer = new XmlSerializer(type);
  107. ms = new MemoryStream(Encoding.Default.GetBytes(xml));
  108. using (sr = new StreamReader(ms, Encoding.Default))
  109. {
  110. return serializer.Deserialize(sr);
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. throw ex;
  116. }
  117. finally
  118. {
  119. sr.Dispose();
  120. sr.Close();
  121. ms.Dispose();
  122. }
  123. }
  124. }
  125. }