实现IConfigurationSectionHandler接口读取配置文件
Code
namespace MySpace.SectionHandler
{
public class ConfigItem
{
string _connType;
string _cmdType;
string _adapterType;
string _readerType;
public string ConnType
{
get { return _connType; }
set { _connType = value; }
}
public string CmdType
{
get { return _cmdType; }
set { _cmdType = value; }
}
public string AdapterType
{
get { return _adapterType; }
set { _adapterType = value; }
}
public string ReaderType
{
get { return _readerType; }
set { _readerType = value; }
}
}
public class CustomSection:IConfigurationSectionHandler
{
public object Create(object parent,object configContext,System.Xml.Node section)
{
List<ConfigItem> list=new List<ConfigItem>();
if(parent!=null){list=(List<ConfigItem>)parent;}
foreach(XmlNode node in section.ChildNodes)
{
if(node.Attributes!=null)
{
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;
}
}
}
namespace MySpace.SectionHandler
{
public class ConfigItem
{
string _connType;
string _cmdType;
string _adapterType;
string _readerType;
public string ConnType
{
get { return _connType; }
set { _connType = value; }
}
public string CmdType
{
get { return _cmdType; }
set { _cmdType = value; }
}
public string AdapterType
{
get { return _adapterType; }
set { _adapterType = value; }
}
public string ReaderType
{
get { return _readerType; }
set { _readerType = value; }
}
}
public class CustomSection:IConfigurationSectionHandler
{
public object Create(object parent,object configContext,System.Xml.Node section)
{
List<ConfigItem> list=new List<ConfigItem>();
if(parent!=null){list=(List<ConfigItem>)parent;}
foreach(XmlNode node in section.ChildNodes)
{
if(node.Attributes!=null)
{
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;
}
}
}
相应的配置文件如下:
Code
<configSections>
<section name="conn" type="MySapce.SectionHandler.CustomSection,CustomSection"/>
</configSections>
<conn>
<myStr adapterType="SqlDataAdapter" cmdType="SqlCommand" connType="SqlConnection" readerType="SqlDataReader"/>
</conn>
<configSections>
<section name="conn" type="MySapce.SectionHandler.CustomSection,CustomSection"/>
</configSections>
<conn>
<myStr adapterType="SqlDataAdapter" cmdType="SqlCommand" connType="SqlConnection" readerType="SqlDataReader"/>
</conn>
页面获取配置信息
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);
}
List<MySapce.SectionHandler.ConfigItem> list = ConfigurationManager.GetSection("conn") as List<ConfigItem>;
Response.Write(list.Count);
foreach (ConfigItem item in list)
{
Response.Write(item.CmdType);
}