| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
-
- using System;
- using System.Xml;
- using System.Reflection;
- using System.Collections.Generic;
- using SHJX.Service.Common.Logging;
- 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;
- private static string fileName;
- public void Load(string source)
- {
- fileName = source;
- configFile = new XmlDocument();
- var settings = new XmlReaderSettings
- {
- IgnoreComments = true //忽略文档里面的注释
- };
- var reader = XmlReader.Create(source, settings);
- configFile.Load(reader);
- }
- /// <summary>
- /// 无参构造
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public T ConvertTo<T>()
- {
- var type = typeof(T);
- if (type.BaseType != typeof(AbstractConfiguration))
- return default;
- var obj = Activator.CreateInstance(type, true);
- var 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.LogInformation("Cannot find any set definition using path [{0}].", xmlPath);
- }
- return setValues;
- }
- /// <summary>
- /// 写入单值
- /// </summary>
- /// <param name="xmlPath"></param>
- /// <param name="value"></param>
- protected void WriteSingleValue(string xmlPath, object value)
- {
- var 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);
- }
- }
- }
|