.Net 自定义应用程序配置 configSections

参考:
http://www.cnblogs.com/wzh206/archive/2013/05/13/3076495.html

 

 web.config配置代码

 

<? xml version="1.0" encoding="utf-8" ?>

<!--
  有关如何配置 ASP.NET 应用程序的详细消息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  
-->

< configuration >
     < configSections >
         < section  name ="mailServerGroup"  type ="configSectionsDemo.Config.MailServerConfigurationHandler, configSectionsDemo, Version=1.0.0.0, Culture=neutral"    />
     </ configSections >
     < mailServerGroup  provider ="www.edong.com" >
         < mailServer  client ="forum.tracefact.net" >
             < address >mail1.tracefact.net </ address >
             < userName >jimmyzhang </ userName >
             < password >123456 </ password >
         </ mailServer >
         < mailServer  client ="blog.tracefact.com" >
             < address >mail2.tracefact.net </ address >
             < userName >webmaster </ userName >
             < password >456789 </ password >
         </ mailServer >
     </ mailServerGroup >
     < system.web >
         < compilation  debug ="true"  targetFramework ="4.0"   />
     </ system.web >

</ configuration >

 

 类代码

 

MailServer

 

 

using System.Collections;

namespace configSectionsDemo.Config
{
     public  class MailServer
    {

         //  存储mailServer的子结点(Address,UserName,Password)的innerText值
        
//  以及属性 Client 的值
         private Hashtable serverNode;

         public MailServer()
        {
            serverNode =  new Hashtable();
        }

         public Hashtable ServerNode
        {
             get {  return serverNode; }
        }

         public  string Client
        {
             get {  return serverNode[ " client "as  string; }
        }

         public  string Address
        {
             get {  return serverNode[ " address "as  string; }
        }

         public  string UserName
        {
             get {  return serverNode[ " userName "as  string; }
        }

         public  string Password
        {
             get {  return serverNode[ " password "as  string; }
        }
    }
}

 

MailServerConfig

 

 

using System.Collections.Generic;

namespace configSectionsDemo.Config
{
     public  class MailServerConfig : List<MailServer>
    {
         private  string provider;      //  对应 mailServerGroup 结点的provider 属性
         public  string Provider
        {
             get {  return provider; }
             set { provider = value; }
        }
    }
}

 

MailServerConfigurationHandler

 

using System.Configuration;
using System.Xml;

namespace configSectionsDemo.Config
{
     //  自定义配置结点 mailServerGroup 的处理程序
     public  class MailServerConfigurationHandler : IConfigurationSectionHandler
    {

         //  section 为 MailServerGroup 结点
         public  object Create( object parent,  object configContext, XmlNode section)
        {

             //  设置方法返回的配置对象,可以是任何类型
            MailServerConfig config =  new MailServerConfig();

             //  获取结点的属性信息       
            config.Provider =
                section.Attributes[ " provider "] ==  null ?  "" : section.Attributes[ " provider "].Value;

             //  获取 MailServer 结点
             foreach (XmlNode child  in section.ChildNodes)
            {

                MailServer server =  new MailServer();

                 //  添加Client属性
                 if (child.Attributes[ " client "] !=  null)
                    server.ServerNode.Add( " client ", child.Attributes[ " client "].Value);

                 //  获取MailServer下的 Name,UserName,Password 结点
                 foreach (XmlNode grandChild  in child.ChildNodes)
                {

                     //  添加文本
                    server.ServerNode.Add(grandChild.Name, grandChild.InnerText);
                }

                 //  将server加入 MailServerConfig
                config.Add(server);
            }
             return config;
        }
    }
}

页面代码

aspx

 

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " WebForm1.aspx.cs "  Inherits = " configSectionsTest.WebForm1 "   %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  runat ="server" >
     < title ></ title >
</ head >
< body >
     < form  id ="form1"  runat ="server" >
     < div >
         < h1 >
            使用 自定义结点 和 自定义处理程序 </ h1 >
         < b >MailServerGroup Provider </ b >:
         < asp:Literal  ID ="ltrServerProvider"  runat ="server" ></ asp:Literal >
         < h2 >
            Mail Server1(Client: < asp:Literal  ID ="ltrClient1"  runat ="server" ></ asp:Literal >) Information: </ h2 >
         < hr  />
         < b >Address </ b >:
         < asp:Literal  ID ="ltrAddress1"  runat ="server" ></ asp:Literal >
         < br  />
         < b >UserName </ b >:
         < asp:Literal  ID ="ltrUserName1"  runat ="server" ></ asp:Literal >< br  />
         < b >Password </ b >:
         < asp:Literal  ID ="ltrPassword1"  runat ="server" ></ asp:Literal >
         < h2 >
            Mail Server2(Client: < asp:Literal  ID ="ltrClient2"  runat ="server" ></ asp:Literal >) Information: </ h2 >
         < hr  />
         < b >Address </ b >:
         < asp:Literal  ID ="ltrAddress2"  runat ="server" ></ asp:Literal >
         < br  />
         < b >UserName </ b >:
         < asp:Literal  ID ="ltrUserName2"  runat ="server" ></ asp:Literal >< br  />
         < b >Password </ b >:
         < asp:Literal  ID ="ltrPassword2"  runat ="server" ></ asp:Literal >
         < br  />
         < br  />
     </ div >
     </ form >
</ body >
</ html >

页面后端代码

 

using System;
using System.Configuration;
using configSectionsDemo.Config;

namespace configSectionsTest
{
     public  partial  class WebForm1 : System.Web.UI.Page
    {
         //  SimpleCustom.aspx.cs
         protected  void Page_Load( object sender, EventArgs e)
        {
             //  获取 mailServerConfig 对象
            MailServerConfig mailConfig = (MailServerConfig)ConfigurationManager.GetSection( " mailServerGroup ");

             //  获取 MailServerGroup 结点的 Provider 属性
            ltrServerProvider.Text = mailConfig.Provider;

             //  获取第一租 MailServer 数据
            ltrClient1.Text = mailConfig[ 0].Client;
            ltrAddress1.Text = mailConfig[ 0].Address;
            ltrUserName1.Text = mailConfig[ 0].UserName;
            ltrPassword1.Text = mailConfig[ 0].Password;

             //  获取第二租 MailServer 数据
            ltrClient2.Text = mailConfig[ 1].Client;
            ltrAddress2.Text = mailConfig[ 1].Address;
            ltrUserName2.Text = mailConfig[ 1].UserName;
            ltrPassword2.Text = mailConfig[ 1].Password;
        }
    }
}

 

也是为了方便阅读参考,特简单摘录在此。代码一看就明白。

 

附上demo源码吧 ,一调试就明了。

http://files.cnblogs.com/yelaiju/configSectionsDemo.rar

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值