/// <summary> /// 加载自定义配置列表 /// </summary> /// <param name="xmlSubPath">配置文件中的相对位置</param> /// <returns></returns> private List <TEntity> GetAppSettings(string xmlSubPath = null) { List <TEntity> result = new List <TEntity>(); foreach (var xmlPath in XmlPaths) { if (!xmlPath.EndsWithIgnoreCase(".xml")) { continue; } XDocument doc = AppSettingsUtils.LoadXml(xmlPath); var appSettings = doc.Elements().Where(s => s.Name.LocalName.EqualsIgnoreCase("AppSettings")); if (appSettings == null) { continue; } List <XElement> elements = appSettings.ToList(); if (string.IsNullOrEmpty(xmlSubPath)) { elements = elements.Elements().Where(s => s.Name.LocalName.EqualsIgnoreCase(this.GetClassName())).ToList(); } else { //var test = appSettings // .Elements().Where(s => s.Name.LocalName.EqualsIgnoreCase("DataSyncConfig")) // .Elements().Where(s => s.Name.LocalName.EqualsIgnoreCase("StationMapping")) // .Elements().Where(s => s.Name.LocalName.EqualsIgnoreCase("StationItem")); var TEntityName = GetTType <TEntity>(); if (!xmlSubPath.EndsWithIgnoreCase(TEntityName)) { xmlSubPath = xmlSubPath + "." + TEntityName; } var arr = xmlSubPath.Split('.'); foreach (var sub in arr) { if (sub.EqualsIgnoreCase("AppSettings")) { continue; } elements = elements.Elements().Where(s => s.Name.LocalName.EqualsIgnoreCase(sub)).ToList(); } } if (elements.Count == 0) { continue; } PropertyInfo[] Propertes = this.GetPropertys(); foreach (XElement element in elements) { var obj = (TEntity)BuildObj(typeof(TEntity), Propertes, element); result.Add(obj); } } return(result); }
/// <summary> /// 读取xml中的key value /// </summary> /// <param name="nv"></param> /// <param name="xmlPath"></param> void ReadFromXML(NameValueCollection nv, string xmlPath) { XDocument doc = AppSettingsUtils.LoadXml(xmlPath); //XDocument.Load(xmlPath); var appSettings = doc.Elements().FirstOrDefault(s => s.Name.LocalName.EqualsIgnoreCase("AppSettings")); if (appSettings == null) { return; } var adds = appSettings.Elements().Where(s => s.Name.LocalName.EqualsIgnoreCase("add")).ToList(); foreach (XElement x in adds) { var key = x.Attributes().FirstOrDefault(s => s.Name.LocalName.EqualsIgnoreCase("key")); var value = x.Attributes().FirstOrDefault(s => s.Name.LocalName.EqualsIgnoreCase("value")); if (key != null && value != null) { if (!nv.AllKeys.Contains(key.Value)) { nv.Add(key.Value, value.Value); } } } }
/// <summary> /// 读取properties中的key value /// </summary> /// <param name="nv"></param> /// <param name="xmlPath"></param> void ReadFromProperties(NameValueCollection nv, string xmlPath) { var arr = AppSettingsUtils.LoadProperties(xmlPath); //File.ReadAllLines(xmlPath); foreach (var item in arr) { //空行 if (string.IsNullOrEmpty(item)) { continue; } //注释 if (item.StartsWith("#")) { continue; } var iIndex = item.IndexOf("="); //无=,第一个字符不能是= if (iIndex < 1) { continue; } var key = item.Substring(0, iIndex).Trim(); var value = item.Substring(iIndex + 1).Trim(); if (!nv.AllKeys.Contains(key)) { nv.Add(key, value); } } }
/// <summary> /// 加载自定义配置 /// </summary> /// <returns></returns> public List <XElement> GetAppSettings() { var list = new List <XElement>(); foreach (var xmlPath in XmlPaths) { if (!xmlPath.EndsWithIgnoreCase(".xml")) { continue; } XDocument doc = AppSettingsUtils.LoadXml(xmlPath); //XDocument.Load(xmlPath); var appSettings = doc.Elements().FirstOrDefault(s => s.Name.LocalName.EqualsIgnoreCase("AppSettings")); if (appSettings == null) { continue; } list.AddRange(appSettings.Elements().ToList()); } return(list); }