XMLReadWrite.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //XML读写类
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml.Serialization;
  9. namespace MvvmScaffoldFrame48.DLL.ConfigTools
  10. {
  11. public static class XMLReadWrite
  12. {
  13. /// <summary>
  14. /// 序列化对象到XML文件
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. /// <param name="data"></param>
  18. /// <param name="filePath"></param>
  19. /// <exception cref="InvalidOperationException"></exception>
  20. public static void SerializeToXml<T>(T data, string filePath)
  21. {
  22. try
  23. {
  24. var serializer = new XmlSerializer(typeof(T));
  25. using (var writer = new StreamWriter(filePath))
  26. {
  27. serializer.Serialize(writer, data);
  28. }
  29. }
  30. catch (Exception ex)
  31. {
  32. throw new InvalidOperationException("XML serialization failed", ex);
  33. }
  34. }
  35. /// <summary>
  36. /// 序列化对象为XML字符串
  37. /// </summary>
  38. /// <typeparam name="T">对象类型</typeparam>
  39. /// <param name="data">要序列化的对象</param>
  40. /// <returns>XML字符串</returns>
  41. /// <exception cref="InvalidOperationException"></exception>
  42. public static string SerializeToString<T>(T data)
  43. {
  44. try
  45. {
  46. var serializer = new XmlSerializer(typeof(T));
  47. using (var writer = new StringWriter())
  48. {
  49. serializer.Serialize(writer, data);
  50. return writer.ToString();
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. throw new InvalidOperationException("XML serialization to string failed", ex);
  56. }
  57. }
  58. /// <summary>
  59. /// 从XML文件反序列化对象
  60. /// </summary>
  61. /// <typeparam name="T"></typeparam>
  62. /// <param name="filePath"></param>
  63. /// <returns></returns>
  64. /// <exception cref="InvalidOperationException"></exception>
  65. public static T DeserializeFromXml<T>(string filePath)
  66. {
  67. try
  68. {
  69. var serializer = new XmlSerializer(typeof(T));
  70. using (var reader = new StreamReader(filePath))
  71. {
  72. return (T)serializer.Deserialize(reader);
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. throw new InvalidOperationException("XML deserialization failed", ex);
  78. }
  79. }
  80. /// <summary>
  81. /// 从XML字符串反序列化对象
  82. /// </summary>
  83. /// <typeparam name="T">对象类型</typeparam>
  84. /// <param name="xmlString">XML字符串</param>
  85. /// <returns>反序列化的对象</returns>
  86. /// <exception cref="InvalidOperationException"></exception>
  87. public static T DeserializeFromString<T>(string xmlString)
  88. {
  89. try
  90. {
  91. var serializer = new XmlSerializer(typeof(T));
  92. using (var reader = new StringReader(xmlString))
  93. {
  94. return (T)serializer.Deserialize(reader);
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. throw new InvalidOperationException("XML deserialization from string failed", ex);
  100. }
  101. }
  102. }
  103. }