获取配置文件中configSections的配置信息!-.

configsections的作用很多!我就不多举了。

首先要注意的一点是!configsections关联的类型在一个进程内只实例化一次,

1、配置文件web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configsections>
<section name="xinyulou" type="config.configurationhandler, config" />
</configsections>

<xinyulou>
<add name="region" value = "心雨楼" />
</xinyulou>


<system.web>
<compilation defaultlanguage="c#" debug="true" />
<customerrors mode="remoteonly" />
<authentication mode="windows" />
<authorization>
<allow users="*" /> <!-- <sectiongroup name="xinyulou"> 允许所有用户 -->
</authorization>
<trace enabled="false" requestlimit="10" pageoutput="false" tracemode="sortbytime" localonly="true" />
<sessionstate mode="inproc" stateconnectionstring="tcpip=127.0.0.1:42424" sqlconnectionstring="data source=127.0.0.1;trusted_connection=yes"
cookieless="false" timeout="20" />
<globalization requestencoding="utf-8" responseencoding="utf-8" />
</system.web>

</configuration>

2、两个重要的类

using system;
using system.configuration;
using system.collections;
using system.xml;

namespace config
{
public class configuration
{
private hashtable m_myregionconfig = new hashtable();
public hashtable myregionconfig
{
get {return m_myregionconfig;}

}
public static configuration getconfig()
{
return (configuration) configurationsettings.getconfig("xinyulou");
}
internal void loadvaluesfromconfigurationxml(xmlnode node)
{

foreach (xmlnode child in node.childnodes)
{
m_myregionconfig.add(child.attributes["name"].value,child.attributes["value"].value);
}

}

}

internal class configurationhandler : iconfigurationsectionhandler
{

public virtual object create(object parent, object context, xmlnode node)
{
configuration config = new configuration();
config.loadvaluesfromconfigurationxml(node);
return config;
}

}
}

3、示例在aspx中用!

private void page_load(object sender, system.eventargs e)
{
response.write(configuration.getconfig().myregionconfig["region"].tostring());
}



configsections的威力很强大。有很多种用法与应用!这里就不再举例了,有问题可与我联系。或在恢复中提出来!
posted @ 2007-03-20 11:02  随风而逝  阅读(841)  评论(0编辑  收藏  举报