XmlHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. namespace Universal;
  2. public class XmlFileHelper
  3. {
  4. public static bool ReadFile<T>(string filePath, out T? output)
  5. {
  6. output = default;
  7. if (string.IsNullOrEmpty(filePath))
  8. return false;
  9. if (!File.Exists(filePath))
  10. return false;
  11. try
  12. {
  13. using StreamReader sr = new(filePath);
  14. XmlSerializer xz = new(typeof(T));
  15. if (xz.Deserialize(sr) is not T temp)
  16. return false;
  17. output = temp;
  18. }
  19. catch
  20. {
  21. return false;
  22. }
  23. return true;
  24. }
  25. public static bool WriteFile(string filePath, object input)
  26. {
  27. if (input == null)
  28. return false;
  29. if (string.IsNullOrEmpty(filePath))
  30. return false;
  31. try
  32. {
  33. FileInfo file = new(filePath);
  34. if (!Directory.Exists(file.Directory!.FullName))
  35. Directory.CreateDirectory(file.Directory!.FullName);
  36. using StreamWriter sw = file.CreateText();
  37. sw.Write(XmlHelper.SerializeXml(input));
  38. }
  39. catch
  40. {
  41. return false;
  42. }
  43. return true;
  44. }
  45. }
  46. public class XmlHelper
  47. {
  48. public static string SerializeXml(object objectToConvert)
  49. {
  50. XmlDocument xmlDocument = new();
  51. XmlSerializer xmlSerializer = new(objectToConvert.GetType());
  52. using (MemoryStream xmlStream = new())
  53. {
  54. xmlSerializer.Serialize(xmlStream, objectToConvert);
  55. xmlStream.Position = 0;
  56. xmlDocument.Load(xmlStream);
  57. }
  58. return xmlDocument.InnerXml;
  59. }
  60. public static bool DeserializeXml<T>(string s, out T? output)
  61. {
  62. output = default;
  63. try
  64. {
  65. using StreamReader sr = new(s, Encoding.UTF8);
  66. XmlSerializer xz = new(typeof(T));
  67. if (xz.Deserialize(sr) is not T t)
  68. return false;
  69. output = t;
  70. }
  71. catch
  72. {
  73. return false;
  74. }
  75. return true;
  76. }
  77. }
  78. /// <summary>
  79. /// Dictionary(支持 XML 序列化)
  80. /// </summary>
  81. /// <typeparam name="TKey">键类型</typeparam>
  82. /// <typeparam name="TValue">值类型</typeparam>
  83. [XmlRoot("XmlDictionary")]
  84. [Serializable]
  85. public class XmlDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable where TKey : notnull
  86. {
  87. #region 构造函数
  88. public XmlDictionary()
  89. { }
  90. public XmlDictionary(IDictionary<TKey, TValue> dictionary) : base(dictionary)
  91. { }
  92. public XmlDictionary(IEqualityComparer<TKey> comparer) : base(comparer)
  93. { }
  94. public XmlDictionary(int capacity) : base(capacity)
  95. { }
  96. public XmlDictionary(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer)
  97. { }
  98. #endregion 构造函数
  99. #region IXmlSerializable Members
  100. public XmlSchema? GetSchema() => null;
  101. /// <summary>
  102. /// 从对象的 XML 表示形式生成该对象(反序列化)
  103. /// </summary>
  104. /// <param name="xr"></param>
  105. public void ReadXml(XmlReader xr)
  106. {
  107. if (xr.IsEmptyElement)
  108. return;
  109. var ks = new XmlSerializer(typeof(TKey));
  110. var vs = new XmlSerializer(typeof(TValue));
  111. xr.Read();
  112. while (xr.NodeType != XmlNodeType.EndElement)
  113. {
  114. xr.ReadStartElement("Item");
  115. xr.ReadStartElement("Key");
  116. var key = (TKey)ks.Deserialize(xr)!;
  117. xr.ReadEndElement();
  118. xr.ReadStartElement("Value");
  119. var value = (TValue)vs.Deserialize(xr)!;
  120. xr.ReadEndElement();
  121. Add(key, value);
  122. xr.ReadEndElement();
  123. xr.MoveToContent();
  124. }
  125. xr.ReadEndElement();
  126. }
  127. /// <summary>
  128. /// 将对象转换为其 XML 表示形式(序列化)
  129. /// </summary>
  130. /// <param name="xw"></param>
  131. public void WriteXml(XmlWriter xw)
  132. {
  133. var ks = new XmlSerializer(typeof(TKey));
  134. var vs = new XmlSerializer(typeof(TValue));
  135. foreach (var key in Keys)
  136. {
  137. xw.WriteStartElement("Item");
  138. xw.WriteStartElement("Key");
  139. ks.Serialize(xw, key);
  140. xw.WriteEndElement();
  141. xw.WriteStartElement("Value");
  142. vs.Serialize(xw, this[key]);
  143. xw.WriteEndElement();
  144. xw.WriteEndElement();
  145. }
  146. }
  147. #endregion IXmlSerializable Members
  148. }