zoukankan      html  css  js  c++  java
  • .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源码吧 ,一调试就明了。

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

  • 相关阅读:
    Git远程仓库的使用(github为例)
    SQL查看数据库中每张表的数据量和总数据量
    HTML简单的注册页面搭建
    java新建日期获取工具
    ArrayList的使用方法技巧(转载)
    UI初级 TextField
    UI初级 Label
    UI入门 纯代码 第一节 UIWindow, UIView
    C 入门 第十一节
    secoclient安装mac版提示系统配置文件写入失败的解决方案
  • 原文地址:https://www.cnblogs.com/yelaiju/p/3080909.html
Copyright © 2011-2022 走看看