实现IConfigurationSectionHandler接口读取配置文件

 

ContractedBlock.gif ExpandedBlockStart.gif Code
namespace MySpace.SectionHandler
ExpandedBlockStart.gifContractedBlock.gif
{
  
public class ConfigItem
ExpandedSubBlockStart.gifContractedSubBlock.gif  
{
        
string _connType;
        
string _cmdType;
        
string _adapterType;
        
string _readerType;
        
public string ConnType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _connType; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _connType = value; }
        }

        
public string CmdType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _cmdType; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _cmdType = value; }
        }

        
public string AdapterType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _adapterType; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _adapterType = value; }
        }

        
public string ReaderType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _readerType; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { _readerType = value; }
        }

  }

  
public class CustomSection:IConfigurationSectionHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
  
public object Create(object parent,object configContext,System.Xml.Node section)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
{
     List
<ConfigItem> list=new List<ConfigItem>();
ExpandedSubBlockStart.gifContractedSubBlock.gif     
if(parent!=null){list=(List<ConfigItem>)parent;}
     
foreach(XmlNode node in section.ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
{
        
if(node.Attributes!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ConfigItem item 
= new ConfigItem();
            item.AdapterType 
= node.Attributes["adapterType"].Value;
            item.CmdType 
= node.Attributes["cmdType"].Value;
            item.ConnType 
= node.Attributes["connType"].Value;
            item.ReaderType 
= node.Attributes["readerType"].Value;
                    list.Add(item); 
        }

     }

     
return list;
  }

}

}

 

相应的配置文件如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  <configSections>
    
<section name="conn" type="MySapce.SectionHandler.CustomSection,CustomSection"/>
  
</configSections>
  
<conn>
    
<myStr adapterType="SqlDataAdapter" cmdType="SqlCommand" connType="SqlConnection" readerType="SqlDataReader"/>
  
</conn>

 

页面获取配置信息

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        List<MySapce.SectionHandler.ConfigItem> list = ConfigurationManager.GetSection("conn"as List<ConfigItem>;
        Response.Write(list.Count);
        
foreach (ConfigItem item in list)
        {
            Response.Write(item.CmdType);
        }

转载于:https://www.cnblogs.com/trampt/archive/2008/08/01/1257999.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值