星火微课系统客户端
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.

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