本文转自: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. DP:Cheapest Palindrome(POJ 3280)

    价值最小回文字符串 题目大意:给你一个字符串,可以删除可以添加,并且每一次对一个字母的操作都带一个权,问你转成回文串最优操作数. 如果这一题我这样告诉你,你毫无疑问知道这一题是LD(Levenshti ...

  2. [Android Pro] PackageManager#getPackageSizeInfo (hide)

    referce to : http://www.baidufe.com/item/8786bc2e95a042320bef.html 计算Android App所占用d的手机内存(RAM)大小.App ...

  3. vs 附加包含目录属性

    如果是在属性页里头添加了路径,则当程序拷贝到其他电脑上头的话,这个包含目录仍然存在,这就是与添加环境变量的区别.如果是通过添加环境变量配置的路径,则换了台电脑,这个路径就没有了,需要重新配置.

  4. vs2013 error c4996: 'fopen': This function or varia

    做opencv练习时,使用vs2013遇到如下错误: ​错误1error C4996: 'fopen': This function or variable may be unsafe. Consid ...

  5. c++ 继承 虚函数与多态性 重载 覆盖 隐藏

    http://blog.csdn.net/lushujun2011/article/details/6827555 2011.9.27 1) 定义一个对象时,就调用了构造函数.如果一个类中没有定义任何 ...

  6. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)

    Problem C: The Trip Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 19  Solved: 3[Submit][Status][Web ...

  7. Java Hour1

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 经验约为0 Hour,请各位不吝赐教. Hour1 : 简单 ...

  8. poj 3517 约瑟夫环

    最简单的约瑟夫环,虽然感觉永远不会考约瑟夫环,但数学正好刷到这部分,跳过去的话很难过 直接粘别人分析了 约瑟夫问题: 用数学方法解的时候需要注意应当从0开始编号,因为取余会等到0解. 实质是一个递推, ...

  9. 监控Spark应用方法简介

    监控Spark应用有很多种方法. Web接口每一个SparkContext启动一个web UI用来展示应用相关的一些非常有用的信息,默认在4040端口.这些信息包括: 任务和调度状态的列表RDD大小和 ...

  10. burpsuite绕过本地javascripte上传文件

    burpsuite绕过本地javascripte上传文件   先是找到图片上传的位置:       上传个asp文件试试,一点击上传就出现:       这样基本就可以确定是本地检测文件后缀名的方式来 ...