AbstractConfiguration.cs 4.9 KB

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