• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
内蒙古峰回路转armyfeng
博客园    首页    新随笔    联系   管理    订阅  订阅

[导入]创建新的配置节

您可以用自己的 XML 配置标记扩展标准的 ASP.NET 配置设置集。若要完成该操作,您必须创建自己的配置节处理程序。该处理程序必须是一个实现 IConfigurationSectionHandler 接口的 .NET Framework 类。节处理程序解释并处理 Web.config 文件特定部分中 XML 标记中定义的设置并根据配置设置返回适当的配置对象。处理程序类返回的配置对象可以是任何数据结构;它不限于任何基配置类或配置格式。
下面的示例定义 IConfigurationSectionHandler 接口。
[Visual Basic]
Namespace System.Web.Configuration
   Public Interface IConfigurationSectionHandler
      Function Create(parent As Object, input As Object, _
         node As XmlNode) As Object
   End Interface
End Namespace
[C#]
namespace System.Web.Configuration
{
   public interface IConfigurationSectionHandler
   {
      public Object Create(Object parent, Object input,
         XmlNode node);
   }
}
您还可以定义自己的节,该节与 <appSettings> 节使用相同的配置处理程序。例如:
<configuration>
   <configSections>
      <sectionGroup name="myGroup">
         <sectionGroup name="nestedGroup">
            <section name="mySection" type=
               "System.Configuration.NameValueSectionHandler,System"/>
         </sectionGroup>
      </sectionGroup>
   </configSections>

   <myGroup>
      <nestedGroup>
         <mySection>
            <add key="key_one" value="1"/>
            <add key="key_two" value="2"/>
         </mySection>
      </nestedGroup>
   </myGroup>
</configuration>
您可以读取上面的示例中定义的新配置节的值,如下:
[Visual Basic]
Dim config As NameValueCollection =     
   ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection")
Response.Write("The value of key_one is " & config("key_one") & "<br>")
Response.Write("The value of key_two is " & config("key_two") & "<br>")
[C#]
NameValueCollection config = (NameValueCollection)
   ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection");
Response.Write("The value of key_one is " + config["key_one"] + "<br>");
Response.Write("The value of key_two is " + config["key_two"] + "<br>");


文章来源:http://www.codeguru.cn/ShowThread.aspx?PostID=1288
posted @ 2006-09-24 16:50  老冯  阅读(67)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3