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

1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
1 rok temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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="stream"></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. /// <returns></returns>
  73. public static object Deserailize(Type type, string filename)
  74. {
  75. try
  76. {
  77. object obj;
  78. if (!System.IO.File.Exists(filename))
  79. {
  80. throw new Exception("指定文件不存在");
  81. }
  82. //Xml格式反序列化
  83. using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  84. {
  85. XmlSerializer formatter = new XmlSerializer(type);
  86. obj = formatter.Deserialize(stream);
  87. stream.Close();
  88. }
  89. return obj;
  90. }
  91. catch (Exception)
  92. {
  93. //Logger.Log4NetHelp.Error("反序列化失败:", e);
  94. return null;
  95. }
  96. }
  97. /// <summary>
  98. /// 将xml转为Datable
  99. /// </summary>
  100. public static DataTable XmlToDataTable(string xmlStr)
  101. {
  102. if (!string.IsNullOrEmpty(xmlStr))
  103. {
  104. StringReader strStream = null;
  105. XmlTextReader xmlrdr = null;
  106. try
  107. {
  108. DataSet ds = new DataSet();
  109. //读取字符串中的信息
  110. strStream = new StringReader(xmlStr);
  111. //获取StrStream中的数据
  112. xmlrdr = new XmlTextReader(strStream);
  113. //ds获取Xmlrdr中的数据
  114. ds.ReadXml(xmlrdr);
  115. return ds.Tables[0];
  116. }
  117. catch (Exception)
  118. {
  119. return null;
  120. }
  121. finally
  122. {
  123. //释放资源
  124. if (xmlrdr != null)
  125. {
  126. xmlrdr.Close();
  127. strStream.Close();
  128. strStream.Dispose();
  129. }
  130. }
  131. }
  132. return null;
  133. }
  134. #endregion
  135. #region 序列化
  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. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  147. serializer.Serialize(sw, obj);
  148. sw.Close();
  149. return sw.ToString();
  150. }
  151. }
  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. }