分类
获取配置文件中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 m 7.0.0.1:42424" sqlConnec | ode="InProc" stateConnection tionString="data source=127. | String="tcpip=12 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 Hasht | able(); |
| public Hashtable MyRegionConfig |
| { |
| get {return m_MyRegionConfig;} |
| } |
| public static Configuration Ge | tConfig() |
| { |
| return (Configuration) Config | urationSettings.GetConfig("xinyulou"); |
| } |
| internal void Load | ValuesFromConfigurationXml(X | mlNode node) |
| { |
| foreach (XmlNode child in nod | e.ChildNodes) |
| { |
| m_MyRegionConfig tributes["value"].Value) | .Add(child.Attributes["name" ; | ].Value,child.At |
| } |
| } |
| } |
| internal class Conf | igurationHandler : IConfigur | ationSectionHandler |
| { |
| public virtual obj | ect Create(Object parent, Ob | ject context, XmlNode node) |
| { |
| Configuration config = new Co | nfiguration(); |
| config.LoadValuesFromConfigur | ationXml(node); |
| return config; |
| } |
| } |
| } |
| 3、示例在aspx中用! |
| private void Page_Lo | ad(object sender, System.Eve | ntArgs e) |
| { |
| Response.Write(Co on"].ToString()); | nfiguration.GetConfig().MyRe | gionConfig["Regi |
| } |