原文地址:通过ConfigurationSection来轻松地加载配置文件

 

最近写了一段自定义的ConfigurationSection继承类,通过该class可以轻松得定义和读取配置文件信息,
注意这里使用的是c# 2.0来实现的,相比1.1必须通过实现IConfigurationSectionHandler接口来自定义配置节点类方便多了
不论是web.config还是app.config,都可以使用ConfigurationManager类加载配置文件中自定义的节点内容。

以下是配置文件的层次结构:

 1【转载】通过ConfigurationSection来轻松地加载配置文件<?xml version="1.0" encoding="utf-8" ?>
 2【转载】通过ConfigurationSection来轻松地加载配置文件<configuration>
 3【转载】通过ConfigurationSection来轻松地加载配置文件  <configSections>
 4【转载】通过ConfigurationSection来轻松地加载配置文件    <section name="orders" type="ConsoleTest.OrdersSection, ConsoleTest"/>
 5【转载】通过ConfigurationSection来轻松地加载配置文件  </configSections>
 6【转载】通过ConfigurationSection来轻松地加载配置文件  <orders companyID="2001">
 7【转载】通过ConfigurationSection来轻松地加载配置文件    <order number="100001" amount="222.22">
 8【转载】通过ConfigurationSection来轻松地加载配置文件      <lineItems warehouseNumber="02">
 9【转载】通过ConfigurationSection来轻松地加载配置文件        <lineItem number="00-000-001" description="wii"/>
10【转载】通过ConfigurationSection来轻松地加载配置文件      </lineItems>
11【转载】通过ConfigurationSection来轻松地加载配置文件    </order>
12【转载】通过ConfigurationSection来轻松地加载配置文件    <order number="300001" amount="33.33">
13【转载】通过ConfigurationSection来轻松地加载配置文件      <lineItems warehouseNumber="99">
14【转载】通过ConfigurationSection来轻松地加载配置文件        <lineItem number="00-000-001" description="xbox 360"/>
15【转载】通过ConfigurationSection来轻松地加载配置文件        <lineItem number="00-000-003" description="playstation 3"/>
16【转载】通过ConfigurationSection来轻松地加载配置文件      </lineItems>
17【转载】通过ConfigurationSection来轻松地加载配置文件    </order>
18【转载】通过ConfigurationSection来轻松地加载配置文件  </orders>
19【转载】通过ConfigurationSection来轻松地加载配置文件</configuration>


注意order和lineItem节点都是允许重复出现的

以下是继承自ConfigurationSection的自定义配置节点类:

  1【转载】通过ConfigurationSection来轻松地加载配置文件public class OrdersSection : ConfigurationSection
  2    }

 

 

原文地址:使用 ConfigurationSection 创建自定义配置节

 

我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来实现自定义配置节,在1.0中当然也可以通过IconfigurationSectionHandler 接口创建自定义配置节!这里我们主要学一下通过ConfigurationSection类来实现简单的配置处理程序.
      先看一下在web.config文件中的配置情况,在这里有两个元素,第一个mysection,有两个属性user,password,第二个也有两个属性element1,和element2。配置比较简单。

【转载】通过ConfigurationSection来轻松地加载配置文件  <!--//////////////////////////////////////////////////////////////////////////////////////////////-->
【转载】通过ConfigurationSection来轻松地加载配置文件  
<configSections>
【转载】通过ConfigurationSection来轻松地加载配置文件    
<sectionGroup name="mygroup">
【转载】通过ConfigurationSection来轻松地加载配置文件      
<section name="mysection"
【转载】通过ConfigurationSection来轻松地加载配置文件                       type
="ConfigSection"
【转载】通过ConfigurationSection来轻松地加载配置文件                        allowDefinition
="Everywhere"
【转载】通过ConfigurationSection来轻松地加载配置文件                         allowLocation
="true"/>
【转载】通过ConfigurationSection来轻松地加载配置文件    
</sectionGroup>
【转载】通过ConfigurationSection来轻松地加载配置文件  
</configSections>
【转载】通过ConfigurationSection来轻松地加载配置文件  
<!--//////////////////////////////////////////////////////////////////////////////////////////////-->
【转载】通过ConfigurationSection来轻松地加载配置文件
【转载】通过ConfigurationSection来轻松地加载配置文件  
<mygroup>
【转载】通过ConfigurationSection来轻松地加载配置文件    
<mysection  user="用户" password="密码">
【转载】通过ConfigurationSection来轻松地加载配置文件      
<element element1="属性1" element2="属性2"></element>
【转载】通过ConfigurationSection来轻松地加载配置文件    
</mysection>
【转载】通过ConfigurationSection来轻松地加载配置文件  
</mygroup>


       理解配置文件结构后,我们就需要用继承自System.Configuration.ConfigurationSection的基类来实现简单的配置类ConfigSection,在2.0中,我们只需要这一个类就能实现完成配置,下面请看代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// ConfigSection 的摘要说明
/// </summary>
public class ConfigSection:ConfigurationSection
{
    
public ConfigSection()
    {
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }
[ConfigurationProperty(
"user",DefaultValue="yanghong",IsRequired=true)]
    
public string User
    {
        
get { return (string)this["user"]; }
        
set { this["user"= value; }
    }

    [ConfigurationProperty(
"password",DefaultValue="password",IsRequired=true)]
    
public string PassWord
    {
        
get {  return (string)this["password"]; }
        
set { this["password"= value; }
    }

    [ConfigurationProperty(
"element")]
    
public elementinfo Element
    {
        
get { return  (elementinfo)this["element"]; }
        
set {this["element"= value; }
    }
}
 
public class elementinfo : ConfigurationElement
{
    
public elementinfo()    { }


    [ConfigurationProperty(
"element1", DefaultValue = "element1", IsRequired = true)]
    
public string Element1
    {
        
get { return (string)this["element1"]; }
    }

    [ConfigurationProperty(
"element2",DefaultValue="element2",IsRequired=true)]
    
public string Element2
    {
        
get { return (string)this["element2"]; }
    }


}



   通过下面的代码就可以获得在配置文件中设置的值了

【转载】通过ConfigurationSection来轻松地加载配置文件ConfigSection config = (ConfigSection)ConfigurationManager.GetSection("mygroup/mysection");
【转载】通过ConfigurationSection来轻松地加载配置文件        Response.Write(
"用户名:"+config.User.ToString() + "密码:" + config.PassWord.ToString() + "元素属性:" + config.Element.Element1.ToString() + config.Element.Element2.ToString());

 

 

相关文章: