本文转自: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. 利用 ffmpeg + ImageMagick + 批处理 生成高品质gif动画

    这几天研究如何生成高品质 gif 动画,重新研究 ffmpeg, 目前有一些自动转换工具,效果不佳. Video_to_320x180.bat 把 out.avi 转换成320x180的 01.avi ...

  2. 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  3. 2.django笔记之缓存,session,cookie,ajax

    一.django缓存 1.缓存 缓存的意义在于把昂贵的计算结果保存起来一遍下次的访问,有缓存的站点的流程大概是这样子的: 给定一个url,检查页面是否在缓存中 如果在,返回缓存的页面 否则,生成该页面 ...

  4. Greedy:Yogurt factory(POJ 2393)

    酸奶工厂 题目大意:酸奶工厂每个星期都要制造酸奶,成本每单位x,然后每个星期要生产y,然后酸奶厂有个巨大的储存室,可以无限储存酸奶,而且酸奶的品质不会变坏,每天储存要每单位花费S,求最小的成本. 简直 ...

  5. springJDBC实现mysql简单分页

    效果图:

  6. 使用webstorm调试node程序

    前言 相信大家接触过不少node代码了,如果你应用的比较初级或者针对你的项目不需要接触过深的node代码,也许你仅仅需要简单的console.log('your variable')就完全满足你的需要 ...

  7. php 用封装类的方法操作数据库和批量删除

    封装类 <?php class DBDA { public $host="localhost"; //服务器地址 public $uid="root"; ...

  8. angularjs教程

    http://www.runoob.com/angularjs/angularjs-routing.htmlA

  9. Scala入门

    搭建环境请参考: http://www.cnblogs.com/super-d2/p/4534208.html 1.交互式编程: adeMacBook-Pro:ssdb-master apple$ s ...

  10. php session跨页面传递 session值丢失问题

    .session_start();应该尽量放置到页面的顶部: .如果php.ini里面没有配置 session Autostart的话,每次会话之前,都得手动开启session:session_sta ...