AbstractConfiguration.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Xml;
  3. using SHJX.Service.Common.Logging;
  4. using System.Collections.Generic;
  5. using Microsoft.Extensions.Logging;
  6. namespace SHJX.Service.Common.ReadXML
  7. {
  8. /// <summary>
  9. /// 抽象配置类。默认使用一个xml文件保存配置信息。每个具体的实现类中应该从该xml的不同节点获取相应的配置信息。
  10. /// </summary>
  11. public abstract class AbstractConfiguration
  12. {
  13. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(AbstractConfiguration));
  14. private XmlDocument configFile;
  15. protected abstract string FileName { get; }
  16. public void Load()
  17. {
  18. configFile = new XmlDocument();
  19. XmlReaderSettings settings = new()
  20. {
  21. IgnoreComments = true //忽略文档里面的注释
  22. };
  23. XmlReader reader = XmlReader.Create(FileName, settings);
  24. configFile.Load(reader);
  25. }
  26. /// <summary>
  27. /// 无参构造
  28. /// </summary>
  29. /// <typeparam name="T"></typeparam>
  30. /// <returns></returns>
  31. public T ConvertTo<T>()
  32. {
  33. Type type = typeof(T);
  34. if (type.BaseType != typeof(AbstractConfiguration))
  35. {
  36. return default;
  37. }
  38. object obj = Activator.CreateInstance(type, true);
  39. AbstractConfiguration baseObj = (AbstractConfiguration)obj;
  40. baseObj.configFile = configFile;
  41. return (T)obj;
  42. }
  43. /// <summary>
  44. /// 有参构造
  45. /// </summary>
  46. /// <typeparam name="T"></typeparam>
  47. /// <param name="args"></param>
  48. /// <returns></returns>
  49. public T ConvertTo<T>(object[] args)
  50. {
  51. var type = typeof(T);
  52. if (type.BaseType != typeof(AbstractConfiguration))
  53. return default;
  54. var obj = Activator.CreateInstance(type, args);
  55. var baseObj = (AbstractConfiguration)obj;
  56. baseObj.configFile = this.configFile;
  57. return (T)obj;
  58. }
  59. /// <summary>
  60. /// 获取单值
  61. /// </summary>
  62. /// <param name="xmlPath">节点地址</param>
  63. /// <returns></returns>
  64. protected string ReadSingleValue(string xmlPath)
  65. {
  66. var xn = configFile.SelectSingleNode(xmlPath);
  67. if (xn != null)
  68. {
  69. switch (xn.NodeType)
  70. {
  71. case XmlNodeType.Attribute:
  72. return xn.Value;
  73. case XmlNodeType.Element:
  74. return xn.InnerText;
  75. }
  76. }
  77. logger.LogError("Failed to find an available value using xPath {0}. ", xmlPath);
  78. return string.Empty;
  79. }
  80. /// <summary>
  81. /// 获取值的集合
  82. /// </summary>
  83. /// <param name="xmlPath"></param>
  84. /// <returns></returns>
  85. protected XmlNodeList ReadListValue(string xmlPath) =>
  86. configFile.SelectNodes(xmlPath);
  87. /// <summary>
  88. /// 获取字典集合
  89. /// </summary>
  90. /// <param name="xmlPath"></param>
  91. /// <returns></returns>
  92. protected IEnumerable<Dictionary<string, string>> ReadSetValue(string xmlPath)
  93. {
  94. var setValues = new List<Dictionary<string, string>>();
  95. var xnList = ReadListValue(xmlPath);
  96. if (xnList != null)
  97. {
  98. for (int i = 0; i < xnList.Count; i++)
  99. {
  100. var xn = xnList[i];
  101. var item = new Dictionary<string, string>();
  102. var xaCollection = xn.Attributes;
  103. if (xaCollection is not null)
  104. {
  105. for (var j = 0; j < xaCollection.Count; j++)
  106. {
  107. item.Add(xaCollection[j].Name, xaCollection[j].Value);
  108. }
  109. }
  110. setValues.Add(item);
  111. }
  112. }
  113. else
  114. {
  115. logger.LogError("Cannot find any set definition using path [{0}].", xmlPath);
  116. }
  117. return setValues;
  118. }
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. /// <param name="xmlPath"></param>
  123. /// <param name="prop"></param>
  124. /// <param name="value"></param>
  125. /// <returns></returns>
  126. public List<string> ReadChildsInnerText(string xmlPath, string prop, string value)
  127. {
  128. XmlNodeList xnList = ReadListValue(xmlPath);
  129. List<string> resList = new();
  130. if (xnList is not null)
  131. {
  132. for (int i = 0; i < xnList.Count; i++)
  133. {
  134. XmlNode xn = xnList[i];
  135. foreach (XmlAttribute item in xn.Attributes)
  136. {
  137. if (item.Name.Equals(prop) && item.Value.Equals(value))
  138. {
  139. foreach (XmlNode node in xn.ChildNodes)
  140. {
  141. resList.Add(node.InnerText);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. return resList;
  148. }
  149. /// <summary>
  150. /// 写入单值
  151. /// </summary>
  152. /// <param name="xmlPath"></param>
  153. /// <param name="value"></param>
  154. protected void WriteSingleValue(string xmlPath, object value)
  155. {
  156. XmlNode xn = configFile.SelectSingleNode(xmlPath);
  157. if (xn != null)
  158. {
  159. switch (xn.NodeType)
  160. {
  161. case XmlNodeType.Attribute:
  162. xn.Value = value.ToString();
  163. break;
  164. case XmlNodeType.Element:
  165. xn.InnerText = value.ToString();
  166. break;
  167. }
  168. }
  169. configFile.Save(FileName);
  170. }
  171. }
  172. }