本文转自:http://www.yongfa365.com/item/configuration-configSections-SingleTagSectionHandler-DictionarySectionHandler-NameValueSectionHandler.html

<?xml version="1.0"?>
<configuration> <configSections>
<!--最简的三种,使用系统Handler,不用写C#代码配置,直接调用-->
<section name="SingleTagSectionHandler" type="System.Configuration.SingleTagSectionHandler"/>
<section name="DictionarySectionHandler" type="System.Configuration.DictionarySectionHandler"/>
<section name="NameValueSectionHandler" type="System.Configuration.NameValueSectionHandler"/> <!--自定义section,需要预先在程序里定义-->
<section name="MyBlogSection" type="AboutCustomConfiguration.MyBlogSection,AboutCustomConfiguration"/>
<section name="MySiteSection" type="AboutCustomConfiguration.MySiteSection,AboutCustomConfiguration"/>
</configSections> <SingleTagSectionHandler yongfa365="http://www.yongfa365.com/" cnblogs="http://www.cnblogs.com/"/> <DictionarySectionHandler>
<add key="yongfa365" value="http://www.yongfa365.com/"/>
<add key="cnblogs" value="http://www.cnblogs.com/"/>
</DictionarySectionHandler> <NameValueSectionHandler>
<add key="yongfa365" value="http://www.yongfa365.com/"/>
<add key="cnblogs" value="http://www.cnblogs.com/"/>
</NameValueSectionHandler> <MyBlogSection>
<blogs>
<add UserName="yongfa365" BlogUrl="http://www.yongfa365.com/" Hits="12345" />
<add UserName="cnblogs" BlogUrl="http://www.cnblogs.com/" Hits="54321" />
</blogs>
</MyBlogSection> <MySiteSection>
<yongfa365 UserName="yongfa365" BlogUrl="http://www.yongfa365.com/" Hits="12345" />
<cnblogs UserName="cnblogs" BlogUrl="http://www.cnblogs.com/" Hits="54321" />
</MySiteSection> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized; namespace AboutCustomConfiguration
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\r\nSingleTagSectionHandler:");
var dicSingleTagSectionHandler = ConfigurationManager.GetSection("SingleTagSectionHandler") as IDictionary;
foreach (DictionaryEntry item in dicSingleTagSectionHandler)
{
Console.WriteLine("Key:{0} Value:{1}", item.Key, item.Value);
} Console.WriteLine("\r\nDictionarySectionHandler:");
var dictDictionarySectionHandler = ConfigurationManager.GetSection("DictionarySectionHandler") as IDictionary;
foreach (string key in dictDictionarySectionHandler.Keys)
{
Console.WriteLine("Key:{0} Value:{1}", key, dictDictionarySectionHandler[key]);
} Console.WriteLine("\r\nNameValueSectionHandler:");
var dictNameValueCollection = ConfigurationManager.GetSection("NameValueSectionHandler") as NameValueCollection;
foreach (string key in dictNameValueCollection.Keys)
{
Console.WriteLine("Key:{0} Value:{1}", key, dictNameValueCollection[key]);
} Console.WriteLine("\r\nMyBlogSection:");
var myBlogSection = ConfigurationManager.GetSection("MyBlogSection") as MyBlogSection;
foreach (Blog item in myBlogSection.Blogs)
{
Console.WriteLine("Key:{0} Value:{1}", item.UserName, item.BlogUrl);
} Console.WriteLine("\r\nMySiteSection:");
var mySiteSection = ConfigurationManager.GetSection("MySiteSection") as MySiteSection;
Console.WriteLine(mySiteSection.CnBlogs.BlogUrl);
Console.WriteLine(mySiteSection.YongFa365.BlogUrl); }
} #region MyBlogSection public class MyBlogSection : ConfigurationSection
{
[ConfigurationProperty("blogs", IsDefaultCollection = false)]
public Blogs Blogs { get { return (Blogs)base["blogs"]; } }
} //[ConfigurationCollection(typeof(Blogs),AddItemName="add")]
public class Blogs : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new Blog();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Blog)element).UserName;
}
} public class Blog : ConfigurationElement
{
#region 配置節設置,設定檔中有不能識別的元素、屬性時,使其不報錯
/// /// 遇到未知屬性時,不報錯 ///
///
///
///
protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
//return base.OnDeserializeUnrecognizedAttribute(name, value);
return true;
} /// /// 遇到未知元素時,不報錯 ///
///
///
///
protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
{
//return base.OnDeserializeUnrecognizedElement(elementName, reader);
return true;
}
#endregion [ConfigurationProperty("UserName", IsRequired = true)]
public string UserName { get { return this["UserName"].ToString(); } } [ConfigurationProperty("BlogUrl", IsRequired = true)]
public string BlogUrl { get { return this["BlogUrl"].ToString(); } } [ConfigurationProperty("Hits", IsRequired = true)]
public int Hits { get { return (int)this["Hits"]; } } }
#endregion #region MySiteSection public class MySiteSection : ConfigurationSection
{
[ConfigurationProperty("cnblogs", IsDefaultCollection = false)]
public Blog CnBlogs { get { return (Blog)base["cnblogs"]; } } [ConfigurationProperty("yongfa365", IsDefaultCollection = false)]
public Blog YongFa365 { get { return (Blog)base["yongfa365"]; } }
} #endregion }

引用: .net自定义configSections的5个示例 http://www.yongfa365.com/item/configuration-configSections-SingleTagSectionHandler-DictionarySectionHandler-NameValueSectionHandler.html

[转].net自定义configSections的5个示例的更多相关文章

  1. C# 基于泛型的自定义线性节点链表集合示例

    本例子实现了如何自定义线性节点集合,具体代码如下: using System; using System.Collections; using System.Collections.Generic; ...

  2. Spring MVC中自定义拦截器的简单示例

    1. 引言 拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter. 我们可以让普通的Bean实现HandlerIntercpetor接口或继承 ...

  3. Android自定义组合控件详细示例 (附完整源码)

    在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...

  4. java 自定义注解,并使用示例

    场景: 对需要校验  手机验证码和短信验证码的controller方法添加 自定义的注解 @CheckType 1. 定义注解 /** * 需要短信.验证码验证方法上的注解 * date: 2018年 ...

  5. 自定义JS控件-简单示例

    1.  业务需求: 制作 一个按钮对象,然后 像 winfrom  那样调用 就可以了: 首先 我们新建一个 MyControls的 JS文件:(插入如下代码) //这里运用的面向对象的思想 ,新建了 ...

  6. PHP实现的自定义图像居中裁剪函数示例

    图像居中裁减的大致思路: 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域.(imagecopyresampled ― 重采样拷贝部分图像并调整大小) 2.将缩放后的图像放置在裁减区域中间 ...

  7. 类库探源——System.Configuration 配置信息处理

    按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型 本篇文章主要两方面的内容 1. 如何使用ConfigurationManager 读取AppSetting ...

  8. CAS自定义登录验证方法

    一.CAS登录认证原理 CAS认证流程如下图: CAS服务器的org.jasig.cas.authentication.AuthenticationManager负责基于提供的凭证信息进行用户认证.与 ...

  9. [039] 微信公众帐号开发教程第15篇-自定义菜单的view类型(访问网页)

    引言及内容概要 距离写上一篇文章<自定义菜单的创建及菜单事件响应>整整过了两个月的时间,那时公众平台还没有开放view类型的菜单.在不久前,微信公众平台悄悄开放了view类型的菜单,却没有 ...

随机推荐

  1. MYSQL 删除字段值为NULL的语法

    2014年9月1日 15:11:05 delete form your_table where your_field is null and your_field1 = '123' ...

  2. Java for LeetCode 031 Next Permutation

    Next Permutation Total Accepted: 33595 Total Submissions: 134095     Implement next permutation, whi ...

  3. HDU 1452 Happy 2004 (逆元+快速幂+积性函数)

    G - Happy 2004 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  4. fedora 损坏,开机出现GRUB 2 启动提示符

    如果设置不当,GRUB 2 可能会加载失败,随后进入启动提示符中.要解决此问题,按如下步骤执行: 1. 列出 GRUB 2 识别的驱动器: grub2> ls 2. 对于一个含有三个分区的 do ...

  5. 《JavaScript高级程序设计》学习笔记

    系统学习JS, 从<JavaScript高级程序设计>入门,通过学习jQuery或者angularJS源码来进阶. 第1章 JavaScript简介 1.JS问世的目的是处理以前由服务器端 ...

  6. Linux 内核中断内幕

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-linuxkernelint/index.html#resources Linux 内核中断内幕 ...

  7. 添加Java文档注释

    一.在Eclipse中add javadoc comment的快捷键为: 快捷键为:ALT + SHIFT +J 二.Window-->Preferences-->General--> ...

  8. PHP数组的使用方法小结

    数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体.数组的每个实体都包含两项:键和值. 一.什么是数组数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体.数组的每个实体 ...

  9. Spark编译安装和运行

    一.环境说明 Mac OSX Java 1.7.0_71 Spark 二.编译安装 tar -zxvf spark-.tgz cd spark- ./sbt/sbt assembly ps:如果之前执 ...

  10. js 事件监听 冒泡事件

    js 事件监听  冒泡事件   的取消 [自己写框架时,才有可能用到] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...