终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Data;
  7. using System.Xml;
  8. using System.Xml.Serialization;
  9. namespace BPASmartClient.Helper
  10. {
  11. /// <summary>
  12. /// Xml序列化与反序列化
  13. /// </summary>
  14. public class XmlUtil
  15. {
  16. #region 反序列化
  17. /// <summary>
  18. /// 反序列化
  19. /// </summary>
  20. /// <param name="type">类型</param>
  21. /// <param name="xml">XML字符串</param>
  22. /// <returns></returns>
  23. public static object Deserialize(Type type, string xml)
  24. {
  25. try
  26. {
  27. using (StringReader sr = new StringReader(xml))
  28. {
  29. XmlSerializer xmldes = new XmlSerializer(type);
  30. return xmldes.Deserialize(sr);
  31. }
  32. }
  33. catch (Exception e)
  34. {
  35. return null;
  36. }
  37. }
  38. /// <summary>
  39. /// 反序列化
  40. /// </summary>
  41. /// <param name="type"></param>
  42. /// <param name="xml"></param>
  43. /// <returns></returns>
  44. public static object Deserialize(Type type, Stream stream)
  45. {
  46. XmlSerializer xmldes = new XmlSerializer(type);
  47. return xmldes.Deserialize(stream);
  48. }
  49. #endregion
  50. #region 序列化
  51. /// <summary>
  52. /// 序列化
  53. /// </summary>
  54. /// <param name="type">类型</param>
  55. /// <param name="obj">对象</param>
  56. /// <returns></returns>
  57. public static string Serializer(Type type, object obj)
  58. {
  59. MemoryStream Stream = new MemoryStream();
  60. XmlSerializer xml = new XmlSerializer(type);
  61. try
  62. {
  63. //序列化对象
  64. xml.Serialize(Stream, obj);
  65. }
  66. catch (InvalidOperationException)
  67. {
  68. throw;
  69. }
  70. Stream.Position = 0;
  71. StreamReader sr = new StreamReader(Stream);
  72. string str = sr.ReadToEnd();
  73. sr.Dispose();
  74. Stream.Dispose();
  75. return str;
  76. }
  77. #endregion
  78. }
  79. }