星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

XmlUtilHelper.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. throw new Exception("指定文件不存在");
  81. //Xml格式反序列化
  82. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  83. {
  84. XmlSerializer formatter = new XmlSerializer(type);
  85. obj = formatter.Deserialize(stream);
  86. stream.Close();
  87. }
  88. return obj;
  89. }
  90. catch (Exception)
  91. {
  92. //Logger.Log4NetHelp.Error("反序列化失败:", e);
  93. return null;
  94. }
  95. }
  96. /// <summary>
  97. /// 将xml转为Datable
  98. /// </summary>
  99. public static DataTable XmlToDataTable(string xmlStr)
  100. {
  101. if (!string.IsNullOrEmpty(xmlStr))
  102. {
  103. StringReader StrStream = null;
  104. XmlTextReader Xmlrdr = null;
  105. try
  106. {
  107. DataSet ds = new DataSet();
  108. //读取字符串中的信息
  109. StrStream = new StringReader(xmlStr);
  110. //获取StrStream中的数据
  111. Xmlrdr = new XmlTextReader(StrStream);
  112. //ds获取Xmlrdr中的数据
  113. ds.ReadXml(Xmlrdr);
  114. return ds.Tables[0];
  115. }
  116. catch (Exception)
  117. {
  118. return null;
  119. }
  120. finally
  121. {
  122. //释放资源
  123. if (Xmlrdr != null)
  124. {
  125. Xmlrdr.Close();
  126. StrStream.Close();
  127. StrStream.Dispose();
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133. /// <summary>
  134. /// 实体类转XML
  135. /// </summary>
  136. /// <typeparam name="T"></typeparam>
  137. /// <param name="obj"></param>
  138. /// <returns></returns>
  139. public static string XmlSerialize<T>(T obj)
  140. {
  141. using (StringWriter sw = new StringWriter())
  142. {
  143. Type t = obj.GetType();
  144. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  145. serializer.Serialize(sw, obj);
  146. sw.Close();
  147. return sw.ToString();
  148. }
  149. }
  150. #endregion
  151. #region 序列化
  152. /// <summary>
  153. /// 序列化
  154. /// </summary>
  155. /// <param name="type">类型</param>
  156. /// <param name="obj">对象</param>
  157. /// <returns></returns>
  158. public static string Serializer(Type type, object obj)
  159. {
  160. MemoryStream Stream = new MemoryStream();
  161. XmlSerializer xml = new XmlSerializer(type);
  162. try
  163. {
  164. //序列化对象
  165. xml.Serialize(Stream, obj);
  166. }
  167. catch (InvalidOperationException)
  168. {
  169. //Logger.Log4NetHelp.Error("序列化失败:", ex);
  170. }
  171. Stream.Position = 0;
  172. StreamReader sr = new StreamReader(Stream);
  173. string str = sr.ReadToEnd();
  174. sr.Dispose();
  175. Stream.Dispose();
  176. return str;
  177. }
  178. #endregion
  179. }
  180. }