using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Xml; namespace CustomConfig
{
public class MailServerConfigHandler:IConfigurationSectionHandler
{
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
MailServerConfig config = new MailServerConfig();
config.Provider = section.Attributes["provider"] == null ? "" : section.Attributes["provider"].Value;
foreach (XmlNode child in section.ChildNodes)
{
MailServer server = new MailServer();
server.Client = child.Attributes["client"] == null ? "" : child.Attributes["client"].Value;
foreach (XmlNode grandChild in child.ChildNodes)
{
server.ServerNode.Add(grandChild.Name, grandChild.InnerText);
}
config.Add(server);
}
return config;
}
} public class MailServer
{
private Hashtable serverNode;
public MailServer()
{
serverNode = new Hashtable();
} public Hashtable ServerNode
{
get { return serverNode; }
} public string Client { get; set; } 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; }
}
} public class MailServerConfig : List<MailServer>
{
public string Provider { get; set; }
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mailServerGroup" type="CustomConfig.MailServerConfigHandler,CustomConfig"/>
</configSections> <mailServerGroup provider="www.edong.com">
<mailServer client="forum.tracefact.net">
<address>addr1</address>
<userName>user1</userName>
<password>pwd1</password>
</mailServer> <mailServer client="blog.tracefact.com">
<address>addr2</address>
<userName>user2</userName>
<password>pwd2</password>
</mailServer>
</mailServerGroup>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomConfig;
using System.Configuration; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
example();
} private static void example()
{
MailServerConfig serverGroup = (MailServerConfig)ConfigurationManager.GetSection("mailServerGroup");
Console.WriteLine("provider:\t"+serverGroup.Provider);
foreach (MailServer config in serverGroup)
{
Console.WriteLine("-------------------------------");
Console.WriteLine("client:\t"+config.Client);
Console.WriteLine("address:\t"+config.Address);
Console.WriteLine("userName:\t"+config.UserName);
Console.WriteLine("password:\t"+config.Password);
}
Console.ReadKey();
}
}
}

第13章 .NET应用程序配置的更多相关文章

  1. 鸟哥的Linux私房菜-第10/11/12/13章(vim程序编辑器、学习bash、正则表达式与文件格式化处理、学习Shell Scripts)

    第10章 vim程序编辑器 可以将vim看做vi的进阶版本,vim可以用颜色或底线等方式来显示出一些特殊的信息. 为何要学习vim?因为: a. 所有的 Unix Like 系统都会内建 vi 文书编 ...

  2. sql server 2000安装程序配置服务器失败

    第一种方法 今天安装SQL Server 2000遇到了个很BT的问题,提示出下: 安装程序配置服务器失败.参考服务器错误日志和C:\Windows\sqlstp.log了解更多信息. 以前进安装目录 ...

  3. ASM:《X86汇编语言-从实模式到保护模式》第13章:保护模式下内核的加载,程序的动态加载和执行

    ★PART1:32位保护模式下内核简易模型 1. 内核的结构,功能和加载 每个内核的主引导程序都会有所不同,因为内核都会有不同的结构.有时候主引导程序的一些段和内核段是可以共用的(事实上加载完内核以后 ...

  4. 敏捷软件开发:原则、模式与实践——第13章 写给C#程序员的UML概述

    第13章 写给C#程序员的UML概述 UML包含3类主要的图示.静态图(static diagram)描述了类.对象.数据结构以及它们之间的关系,藉此表现出了软件元素间那些不变的逻辑结构.动态图(dy ...

  5. 第13章 Salesforce Lightning应用程序

    13.1 Lightning应用程序 13.1.1 什么是闪电应用程序 Salesforce应用程序有两种风格:经典应用程序和闪电应用程序.经典应用程序在Salesforce Classic中创建和管 ...

  6. Linux就这个范儿 第13章 打通任督二脉

    Linux就这个范儿 第13章 打通任督二脉 0111010110……你有没有想过,数据从看得见或看不见的线缆上飞来飞去,是怎么实现的呢?数据传输业务的未来又在哪里?在前面两章中我们学习了Linux网 ...

  7. 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化

    第13章 综合技术 13.1 使用CrashHandler来获取应用的Crash信息 (1)应用发生Crash在所难免,但是如何采集crash信息以供后续开发处理这类问题呢?利用Thread类的set ...

  8. 【STM32H7教程】第13章 STM32H7启动过程详解

    完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第13章       STM32H7启动过程详解 本章教 ...

  9. 【RL-TCPnet网络教程】第13章 RL-TCPnet之TCP服务器

    第13章      RL-TCPnet之TCP服务器 本章节为大家讲解RL-TCPnet的TCP服务器实现,学习本章节前,务必要优先学习第12章TCP传输控制协议基础知识.有了这些基础知识之后,再搞本 ...

随机推荐

  1. C++设计模式-Flyweight享元模式

    Flyweight享元模式 作用:运用共享技术有效地支持大量细粒度的对象. 内部状态intrinsic和外部状态extrinsic: 1)Flyweight模式中,最重要的是将对象分解成intrins ...

  2. C# 6.0 特性

    C#6.0主要提供了一些语法糖,另外还提供了新的编译器Roslyn地址https://github.com/dotnet/roslyn 一下列举几个新增的语法糖: 1.构造一个类: public cl ...

  3. Python-10 字典

    #1 创建 dict1={'欢欢':'i love','小高高':'you'} dict2={1:'one',2:'two',3:'three'} dict3={} #2 访问元素 print('欢欢 ...

  4. Java-生成指定长度验证码的一种简单思路

     前言:以前做过的一个项目,刚开的时候始验证码是在前端生成前端验证的,后来觉得不靠谱,另外就是找回密码的功能也需要发送邮件和短信的验证码,所以,验证码就必须在后端生成并且保存到应用会话中才行了!所以, ...

  5. Request与session与application的区别

    (1)request的setAttribute与getAttribute方法一般都是成对出现的,首先通过setAttribute方法设置属性与属性值,然后通过getAttribute方法根据属性获取到 ...

  6. 500lines项目简介

    "500行或更少" "What I cannot create, I do not understand." -- Richard Feynman <50 ...

  7. 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。

    (转自:http://blog.sina.com.cn/s/blog_5eca668b01018949.html)定义一个类,来对远程X.509证书的验证,进行处理,返回为true.我们要自己定义一个 ...

  8. linux系统limit知识

    1.limit限制每个shell进程和它的子进程对资源的占用.每个登录,还是每个进程? 比如ulimit -n 10240 可以临时改变当前session的shell进程的文件句柄数限制,退出当前窗口 ...

  9. PIC32MZ tutorial -- UART Communication

    At this moment, I accomplish the interface of UART communication for PIC32MZ EC Starter Kit. This in ...

  10. js语言精粹读书笔记一

    一.语法 1.