| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Xml;
- using SHJX.Service.Common.Logging;
- using System.Collections.Generic;
- using Microsoft.Extensions.Logging;
- namespace SHJX.Service.Common.ReadXML
- {
- /// <summary>
- /// 抽象配置类。默认使用一个xml文件保存配置信息。每个具体的实现类中应该从该xml的不同节点获取相应的配置信息。
- /// </summary>
- public abstract class AbstractConfiguration
- {
- private static readonly ILogger logger = LogFactory.BuildLogger(typeof(AbstractConfiguration));
- private XmlDocument configFile;
- protected abstract string FileName { get; }
- public void Load()
- {
- configFile = new XmlDocument();
- XmlReaderSettings settings = new()
- {
- IgnoreComments = true //忽略文档里面的注释
- };
- XmlReader reader = XmlReader.Create(FileName, settings);
- configFile.Load(reader);
- }
- /// <summary>
- /// 无参构造
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public T ConvertTo<T>()
- {
- Type type = typeof(T);
- if (type.BaseType != typeof(AbstractConfiguration))
- {
- return default;
- }
- object obj = Activator.CreateInstance(type, true);
- AbstractConfiguration baseObj = (AbstractConfiguration)obj;
- baseObj.configFile = configFile;
- return (T)obj;
- }
- /// <summary>
- /// 有参构造
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="args"></param>
- /// <returns></returns>
- public T ConvertTo<T>(object[] args)
- {
- var type = typeof(T);
- if (type.BaseType != typeof(AbstractConfiguration))
- return default;
- var obj = Activator.CreateInstance(type, args);
- var baseObj = (AbstractConfiguration)obj;
- baseObj.configFile = this.configFile;
- return (T)obj;
- }
- /// <summary>
- /// 获取单值
- /// </summary>
- /// <param name="xmlPath">节点地址</param>
- /// <returns></returns>
- protected string ReadSingleValue(string xmlPath)
- {
- var xn = configFile.SelectSingleNode(xmlPath);
- if (xn != null)
- {
- switch (xn.NodeType)
- {
- case XmlNodeType.Attribute:
- return xn.Value;
- case XmlNodeType.Element:
- return xn.InnerText;
- }
- }
- logger.LogError("Failed to find an available value using xPath {0}. ", xmlPath);
- return string.Empty;
- }
- /// <summary>
- /// 获取值的集合
- /// </summary>
- /// <param name="xmlPath"></param>
- /// <returns></returns>
- protected XmlNodeList ReadListValue(string xmlPath) =>
- configFile.SelectNodes(xmlPath);
- /// <summary>
- /// 获取字典集合
- /// </summary>
- /// <param name="xmlPath"></param>
- /// <returns></returns>
- protected IEnumerable<Dictionary<string, string>> ReadSetValue(string xmlPath)
- {
- var setValues = new List<Dictionary<string, string>>();
- var xnList = ReadListValue(xmlPath);
- if (xnList != null)
- {
- for (int i = 0; i < xnList.Count; i++)
- {
- var xn = xnList[i];
- var item = new Dictionary<string, string>();
- var xaCollection = xn.Attributes;
- if (xaCollection is not null)
- {
- for (var j = 0; j < xaCollection.Count; j++)
- {
- item.Add(xaCollection[j].Name, xaCollection[j].Value);
- }
- }
- setValues.Add(item);
- }
- }
- else
- {
- logger.LogError("Cannot find any set definition using path [{0}].", xmlPath);
- }
- return setValues;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="xmlPath"></param>
- /// <param name="prop"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public List<string> ReadChildsInnerText(string xmlPath, string prop, string value)
- {
- XmlNodeList xnList = ReadListValue(xmlPath);
- List<string> resList = new();
- if (xnList is not null)
- {
- for (int i = 0; i < xnList.Count; i++)
- {
- XmlNode xn = xnList[i];
- foreach (XmlAttribute item in xn.Attributes)
- {
- if (item.Name.Equals(prop) && item.Value.Equals(value))
- {
- foreach (XmlNode node in xn.ChildNodes)
- {
- resList.Add(node.InnerText);
- }
- }
- }
- }
- }
- return resList;
- }
- /// <summary>
- /// 写入单值
- /// </summary>
- /// <param name="xmlPath"></param>
- /// <param name="value"></param>
- protected void WriteSingleValue(string xmlPath, object value)
- {
- XmlNode xn = configFile.SelectSingleNode(xmlPath);
- if (xn != null)
- {
- switch (xn.NodeType)
- {
- case XmlNodeType.Attribute:
- xn.Value = value.ToString();
- break;
- case XmlNodeType.Element:
- xn.InnerText = value.ToString();
- break;
- }
- }
- configFile.Save(FileName);
- }
- }
- }
|