1. 首页 > 电脑教程 > 通过通用组件来完成节点配置及读取操作

通过通用组件来完成节点配置及读取操作

在app.config和web.config中我们通常要用自定义节点来完成想要扩展的配置内容,以前我们需要继承ConfigurationSection,ConfigurationElementCollection,ConfigurationElement这些类来完成配置信息的读取代码比较繁琐复杂。后来有了linq to xml 通过它我们很容易查询出配置信息,但是这样却少了类的定义,只能通过XmlNode来读取或者填充我们定义的类。一、原来自定配置文件的编写方式:01 1、定义类型比较繁琐 02 03 internal class AOPConfigurationSection : ConfigurationSection 04 { 05 [ConfigurationProperty("", IsDefaultCollection = true)] 06 public AopElementCollection Aops //需要定义这里略 07 { 08 get 09 { 10 return (AopElementCollection)base[""]; 11 } 12 } 13 } 1 2、LINQ TO XML查询 2 3 XElement xelement = XElement.Parse(xml); 4 var name = from e in xelement.Elements("b") 5 let s = e.Element("e") 6 select s.Attribute("name").Value; 二、通用配置组件介绍引用:DefinitionConfig.dll对象:DisplayConfigName特性(对应节点名称)ReadSection类(初始化节点)ConfigSectionsHelper类(配置解析类)Sections类(配置集合类)特点:根据自定义类型读取配置文件注意:定义属性为string或class类型,集合为泛型Sections类方法:GetConfigSection();//读取唯一节点类型GetConfigSectionChild();//读取包含子节点类型1 1.自定义类型 2 3 public class ConnConfig 4 { 5 [DisplayConfigName("name")]//配置文件中名称为name 6 public string Name { get; set; } 7 public string str{ get; set; }//如果没有声明特性,那么配置文件名称为属性名称str 8 } 01 2.单节点(不含子节点) 02 03 //这里section 中type属性为DefinitionConfig.ConfigSectionsHelper,DefinitionConfig 04 05 06
07 08 09 10 11 12 或 13 14 15 db 16 17 connstring 18 19 20 上面我们只配置了一个connstr节点。没有任何子节点。注:此节点只能有一个,所以不能多个connstr。 21 22 我们把这个配置读取为ConnConfig类型。 23 24 ReadSection rs = new ReadSection("connstr");//实例化ReadSection,参数为节点名称 25 26 ConnConfig conn= rs.GetConfigSection();//通过GetConfigSection读取配置 27 28 Console.WriteLine(conn.Name);//验证是否读取到 01 3、多节点(含子节点) 02 03 04
05 06 07 08 09 10 11 12 13 14 connstring 15 16 17 18 19 20 sqlite 21 22 connstring 23 24 25 26 27 28 ReadSection rs = new ReadSection("connstrs");//读取connstrs节点 29 var con = rs.GetConfigSectionChild();//GetConfigSectionChild读取子节点配置,注:只要有子节点配置都需要用这个来读取 30 foreach (var item in con) 31 { 32 Console.WriteLine(item.Name); 33 Console.WriteLine(item.str); 34 } 01 4、属性为自定义类型(含多个子节点) 02 03 public class ConnConfig 04 { 05 [DisplayConfigName("name")] 06 public string Name { get; set; } 07 public ConnString str { get; set; }//定义为类型 08 } 09 10 public class ConnString { 11 public string name { get; set; } 12 public string type { get; set; } 13 } 14 15 16 17 oracle 18 19 oracle10 20 21 22 23 24 25 ReadSection rs = new ReadSection("connstrs"); 26 var con = rs.GetConfigSectionChild(); 27 Console.WriteLine(con[0].str.name);//oracledb 01 5、属性为自定义集合类型(子节点集合) 02 03 public class ConnConfig 04 { 05 [DisplayConfigName("name")] 06 public string Name { get; set; } 07 public ConnString str { get; set; } 08 public Sections aa { get; set; }//定义集合 09 } 10 11 public class ConnString { 12 public string name { get; set; } 13 public string type { get; set; } 14 } 15 16 public class AA{ 17 public string name{get;set;} 18 } 19 20 21 22 oracle 23 24 oracle10 25 26 27 28 2 29 30 31 32 33 34 ReadSection rs = new ReadSection("connstrs"); 35 var con = rs.GetConfigSectionChild(); 36 foreach (var item in con[0].aa) 37 { 38 Console.WriteLine(item.name); 39 } 01 6、属性为自定义多个集合类型(多子节点集合) 02 03 public class ConnConfig 04 { 05 [DisplayConfigName("name")] 06 public string Name { get; set; } 07 public ConnString str { get; set; } 08 public Sections aa { get; set; } 09 } 10 11 public class ConnString { 12 public string name { get; set; } 13 public string type { get; set; } 14 } 15 16 public class AA 17 { 18 public string name { get; set; } 19 public Sections bb { get; set; } 20 } 21 22 public class BB 23 { 24 public string type { get; set; } 25 } 26 27 28 29 oracle 30 31 oracle10 32 33 34 35 36 37 2 38 39 40 41 42 43 44 ReadSection rs = new ReadSection("connstrs"); 45 var con = rs.GetConfigSectionChild(); 46 foreach (var item in con[0].aa) 47 { 48 Console.WriteLine(item.name); 49 } 1 7、配置外部config 2 3
4 5 6 7 ReadSection rs = new ReadSection("mySection");//读取节点 8 var list = rs.GetConfigSectionChild();

组件下载:DefinitionConfig

组件Demo:ReadConfigDemo

声明:希维路由器教程网提供的内容,仅供网友学习交流,如有侵权请与我们联系删除,谢谢。ihuangque@qq.com
本文地址:https://www.ctrlcv.com.cn/diannao/169323122210576.html