轻松完成自定义节点配置及读取操作(通用组件)

      在app.config和web.config中我们通常要用自定义节点来完成想要扩展的配置内容,以前我们需要继承ConfigurationSection,ConfigurationElementCollection,ConfigurationElement这些类来完成配置信息的读取代码比较繁琐复杂。后来有了linq to xml 通过它我们很容易查询出配置信息,但是这样却少了类的定义,只能通过XmlNode来读取或者填充我们定义的类。

一、原来自定配置文件的编写方式:

1、定义类型比较繁琐

internal class AOPConfigurationSection : ConfigurationSection 
    { 
        [ConfigurationProperty("", IsDefaultCollection = true)] 
        public AopElementCollection Aops //需要定义这里略 
        { 
            get 
            { 
                return (AopElementCollection)base[""]; 
            } 
        } 
    }


2、LINQ TO XML查询

XElement xelement = XElement.Parse(xml);  
var name = from e in xelement.Elements("b")  
                 let s = e.Element("e")  
                 select s.Attribute("name").Value;

 

二、通用配置组件介绍

引用:DefinitionConfig.dll

对象:

        DisplayConfigName特性(对应节点名称)

        ReadSection类(初始化节点)

        ConfigSectionsHelper类(配置解析类)

        Sections类(配置集合类)

特点:根据自定义类型读取配置文件

注意:定义子节点属性类型为Sections泛型集合类

方法:GetConfigSection<ConnConfig>();//读取唯一节点类型

        GetConfigSectionChild<ConnConfig>();//读取包含子节点类型

1.自定义类型

public class ConnConfig 
{ 
    [DisplayConfigName("name")]//配置文件中名称为name 
    public string Name { get; set; } 
    public string str{ get; set; }//如果没有声明特性,那么配置文件名称为属性名称str 
}


2.单节点(不含子节点)

//这里section 中type属性为DefinitionConfig.ConfigSectionsHelper,DefinitionConfig

<configSections> 
    <section name="connstr" type="DefinitionConfig.ConfigSectionsHelper,DefinitionConfig"/>

</configSections>

<connstr name="db" str="connstring"></connstr>

或

<connstr str="connstring"> 
   <name>db</name>

   <str>connstring</str> 
</connstr>

上面我们只配置了一个connstr节点。没有任何子节点。注:此节点只能有一个,所以不能多个connstr。

我们把这个配置读取为ConnConfig类型。

ReadSection rs = new ReadSection("connstr");//实例化ReadSection,参数为节点名称

ConnConfig conn= rs.GetConfigSection<ConnConfig>();//通过GetConfigSection读取配置

Console.WriteLine(conn.Name);//验证是否读取到


3、多节点(含子节点)

<configSections> 
    <section name="connstrs" type="DefinitionConfig.ConfigSectionsHelper,DefinitionConfig"/>

</configSections>

<connstrs>

<conn name=”sql” str=”connstring”></conn>

<conn name=”mysql”>

      <str>connstring</str>

</conn>

<conn>

      <name>sqlite</name>

      <str>connstring</str>

</conn>

</connstrs>

ReadSection rs = new ReadSection("connstrs");//读取connstrs节点 
var con = rs.GetConfigSectionChild<ConnConfig>();//GetConfigSectionChild读取子节点配置,注:只要有子节点配置都需要用这个来读取 
foreach (var item in con) 
{ 
      Console.WriteLine(item.Name); 
      Console.WriteLine(item.str); 
}


4、属性为自定义类型(含多个子节点)

public class ConnConfig 
   { 
       [DisplayConfigName("name")] 
       public string Name { get; set; } 
       public ConnString str { get; set; }//定义为类型 
   }

public class ConnString { 
        public string name { get; set; } 
        public string type { get; set; } 
    }

<connstrs> 
  <con> 
    <name>oracle</name> 
    <str name="oracledb"> 
      <type>oracle10</type> 
    </str> 
  </con>

</connstrs>

ReadSection rs = new ReadSection("connstrs"); 
var con = rs.GetConfigSectionChild<ConnConfig>(); 
Console.WriteLine(con[0].str.name);//oracledb


5、属性为自定义集合类型(子节点集合)

public class ConnConfig 
    { 
        [DisplayConfigName("name")] 
        public string Name { get; set; } 
        public ConnString str { get; set; } 
        public Sections<AA> aa { get; set; }//定义集合 
    }

public class ConnString { 
     public string name { get; set; } 
     public string type { get; set; } 
}

public class AA{ 
        public string name{get;set;} 
    }

<connstrs> 
    <con> 
      <name>oracle</name> 
      <str name="oracledb"> 
        <type>oracle10</type> 
      </str> 
      <aa name="1"></aa> 
      <aa> 
        <name>2</name> 
      </aa>     
    </con>

</connstrs>

ReadSection rs = new ReadSection("connstrs"); 
var con = rs.GetConfigSectionChild<ConnConfig>(); 
foreach (var item in con[0].aa) 
{ 
     Console.WriteLine(item.name); 
}


6、属性为自定义多个集合类型(多子节点集合)

public class ConnConfig 
  { 
      [DisplayConfigName("name")] 
      public string Name { get; set; } 
      public ConnString str { get; set; } 
      public Sections<AA> aa { get; set; } 
  } 

  public class ConnString { 
      public string name { get; set; } 
      public string type { get; set; } 
  } 

  public class AA 
  { 
      public string name { get; set; } 
      public Sections<BB> bb { get; set; } 
  } 

  public class BB 
  { 
      public string type { get; set; } 
  }

<connstrs> 
    <con> 
      <name>oracle</name> 
      <str name="oracledb"> 
        <type>oracle10</type> 
      </str> 
      <aa name="1"> 
        <bb type="type1"></bb> 
      </aa> 
      <aa> 
        <name>2</name> 
        <bb type="type2"></bb> 
      </aa>     
    </con>

</connstrs>

ReadSection rs = new ReadSection("connstrs"); 
var con = rs.GetConfigSectionChild<ConnConfig>(); 
foreach (var item in con[0].aa) 
{ 
      Console.WriteLine(item.name); 
}


7、配置外部config

<section name="mySection" type="DefinitionConfig.ConfigSectionsHelper,DefinitionConfig" requirePermission="false" restartOnExternalChanges="false"/>

<mySection configSource="mySection.config"/>

ReadSection rs = new ReadSection("mySection");//读取节点 
var list = rs.GetConfigSectionChild<ConfigItem>();

 

组件下载:DefinitionConfig

组件Demo:ReadConfigDemo

posted @ 2010-12-06 17:14  Dacey  Views(2203)  Comments(10Edit  收藏  举报