星火批注
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XmlUtilHelper.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace Common.system
  7. {
  8. /// <summary>
  9. /// 通用XML序列化和反序列化工具类
  10. /// 创建人:赵耀
  11. /// 创建时间:2018年11月16日
  12. /// </summary>
  13. public static class XmlUtilHelper
  14. {
  15. #region 反序列化
  16. /// <summary>
  17. /// 反序列化
  18. /// </summary>
  19. /// <param name="type">类型</param>
  20. /// <param name="xml">XML字符串</param>
  21. /// <returns></returns>
  22. public static object Deserialize(Type type, string xml)
  23. {
  24. try
  25. {
  26. using (StringReader sr = new StringReader(xml))
  27. {
  28. XmlSerializer xmldes = new XmlSerializer(type);
  29. return xmldes.Deserialize(sr);
  30. }
  31. }
  32. catch (Exception)
  33. {
  34. return null;
  35. }
  36. }
  37. /// <summary>
  38. /// 反序列化
  39. /// </summary>
  40. /// <param name="type"></param>
  41. /// <param name="xml"></param>
  42. /// <returns></returns>
  43. public static object Deserialize(Type type, Stream stream)
  44. {
  45. XmlSerializer xmldes = new XmlSerializer(type);
  46. return xmldes.Deserialize(stream);
  47. }
  48. /// <summary>
  49. /// 反序列化实体
  50. /// </summary>
  51. /// <typeparam name="T"></typeparam>
  52. /// <param name="strXML"></param>
  53. /// <returns></returns>
  54. public static T DESerializer<T>(string strXML) where T : class
  55. {
  56. try
  57. {
  58. using (StringReader sr = new StringReader(strXML))
  59. {
  60. XmlSerializer serializer = new XmlSerializer(typeof(T));
  61. return serializer.Deserialize(sr) as T;
  62. }
  63. }
  64. catch (Exception)
  65. {
  66. return null;
  67. }
  68. }
  69. /// <summary>
  70. /// 从xml序列中反序列化
  71. /// </summary>
  72. /// <param name="objname"></param>
  73. /// <returns></returns>
  74. public static object Deserailize(Type type, string filename)
  75. {
  76. try
  77. {
  78. object obj;
  79. if (!System.IO.File.Exists(filename))
  80. {
  81. throw new Exception("指定文件不存在");
  82. }
  83. //Xml格式反序列化
  84. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  85. {
  86. XmlSerializer formatter = new XmlSerializer(type);
  87. obj = formatter.Deserialize(stream);
  88. stream.Close();
  89. }
  90. return obj;
  91. }
  92. catch (Exception)
  93. {
  94. //Logger.Log4NetHelp.Error("反序列化失败:", e);
  95. return null;
  96. }
  97. }
  98. /// <summary>
  99. /// 将xml转为Datable
  100. /// </summary>
  101. public static DataTable XmlToDataTable(string xmlStr)
  102. {
  103. if (!string.IsNullOrEmpty(xmlStr))
  104. {
  105. StringReader StrStream = null;
  106. XmlTextReader Xmlrdr = null;
  107. try
  108. {
  109. DataSet ds = new DataSet();
  110. //读取字符串中的信息
  111. StrStream = new StringReader(xmlStr);
  112. //获取StrStream中的数据
  113. Xmlrdr = new XmlTextReader(StrStream);
  114. //ds获取Xmlrdr中的数据
  115. ds.ReadXml(Xmlrdr);
  116. return ds.Tables[0];
  117. }
  118. catch (Exception)
  119. {
  120. return null;
  121. }
  122. finally
  123. {
  124. //释放资源
  125. if (Xmlrdr != null)
  126. {
  127. Xmlrdr.Close();
  128. StrStream.Close();
  129. StrStream.Dispose();
  130. }
  131. }
  132. }
  133. return null;
  134. }
  135. #endregion
  136. #region 序列化
  137. /// <summary>
  138. /// 实体类转XML
  139. /// </summary>
  140. /// <typeparam name="T"></typeparam>
  141. /// <param name="obj"></param>
  142. /// <returns></returns>
  143. public static string XmlSerialize<T>(T obj)
  144. {
  145. using (StringWriter sw = new StringWriter())
  146. {
  147. Type t = obj.GetType();
  148. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  149. serializer.Serialize(sw, obj);
  150. sw.Close();
  151. return sw.ToString();
  152. }
  153. }
  154. /// <summary>
  155. /// 序列化
  156. /// </summary>
  157. /// <param name="type">类型</param>
  158. /// <param name="obj">对象</param>
  159. /// <returns></returns>
  160. public static string Serializer(Type type, object obj)
  161. {
  162. MemoryStream Stream = new MemoryStream();
  163. XmlSerializer xml = new XmlSerializer(type);
  164. try
  165. {
  166. //序列化对象
  167. xml.Serialize(Stream, obj);
  168. }
  169. catch (InvalidOperationException)
  170. {
  171. //Logger.Log4NetHelp.Error("序列化失败:", ex);
  172. }
  173. Stream.Position = 0;
  174. StreamReader sr = new StreamReader(Stream);
  175. string str = sr.ReadToEnd();
  176. sr.Dispose();
  177. Stream.Dispose();
  178. return str;
  179. }
  180. #endregion
  181. }
  182. }