C# 自定义Section
一、在App.config中自定义Section,这个使用了SectionGroup
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <configSections>
<sectionGroup name="IpTables">
<section name="IPs" type="Section.Test.MySectionHandler,Section.Test"/>
</sectionGroup>
</configSections> <IpTables>
<IPs>
<add key="ip" value="127.0.0.1"/>
<add key="port" value="8888"/> </IPs> </IpTables>
</configuration>
xml中的section 需要显示配置自定义的处理程序,即type属性
二、创建处理程序 MySectionHandler
//实现 IConfigurationSectionHandler接口,并且读取自定义Section
public class MySectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
var dic= new Dictionary<string, string>();
//可能会出现注释,所以需要显示过滤xml元素
foreach (XmlElement childNode in section.ChildNodes.OfType<XmlElement>())
{ dic.Add(childNode.Attributes["key"].InnerText,childNode.Attributes["value"].InnerText); }
return dic;
}
}
执行处理程序代码如下:
var dic= ConfigurationManager.GetSection("IpTables/IPs") as IDictionary<string,string>;
注意事项:
1.获取自定义Section,如果是SectionGroup,则需要 SectionGroup/Section 这种格式获取
2.一般该代码写在应用程序初始化处,只加载一次,然后将其值缓存至内存中即可使用
3.<configSections> 元素必须是 configuration 元素的第一个子元素。
三、如何自定义比如log4net.config中那样的节点?
没难度,其实就是基本的xml操作了。
C# 自定义Section的更多相关文章
- 取到 tableview 自定义section header 上的button
在自定义的组头上,添加了一个button,在点击cell是想取到相应的组头上的button来进行操作时(比如说隐藏.是否响应点击事件等)时,我遇到了取不到所有button的问题,试过了常规的通过vie ...
- 配置文件_自定义section标签获取数据
前言:为了节约时间,先只粘贴关键代码: 1-添加section标签,name为自定义标签名称,type为:命名空间+类型,程序集名称 <section name="watchModel ...
- web.config configSections自定义section
1.web.config 配置文件设置 <configSections> <!-- For more information on Entity Framework configur ...
- 自定义Section
转载 :http://www.cnblogs.com/gaobing/p/6047746.html <configSections> 元素必须是 configuration 元素的第一个子 ...
- config中自定义配置
1. 定义自己的KeyValue <section name="TestKeyValue" type="System.Configuration.NameValue ...
- [转].net自定义configSections的5个示例
本文转自:http://www.yongfa365.com/item/configuration-configSections-SingleTagSectionHandler-DictionarySe ...
- C#读取自定义的config
今天说下C#读写自定义config文件的各种方法.由于这类文章已经很多,但是大多数人举例子都是默认的在app.confg或者web.config进行读写,而不是一般的XML文件,我主要写的是一般的Xm ...
- iOS 资源大全
这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...
- UrlRewrite(URL重写)--ASP.NET中的实现
概述 今天看了下URL重写的实现,主要看的是MS 的URL Rewrite. URL重写的优点有:更友好的URL,支持老版本的URL URL重写的缺点有:最主要的缺点是性能低下,因为如果要支持无后缀的 ...
随机推荐
- Docker 1.12 集群
环境介绍 虚拟机两台,vmware ,网络为NAT node139:192.168.190.139 Node140: 192.168.190.140 设置hostname 以139为例 ...
- Android资源(图片)命名规范
(转自:http://www.jb51.net/article/38796.htm) 图片命名注意: 1,不能以下划线("_")开头: 2,以数字加下划线("[0-9]_ ...
- Xamarin Android 所见即所得问题
运行Xamarin 时出现以下问题. The layout could not be loaded : The operation failed due to an internal error : ...
- 电信行业的BI应用
截至2015年年底,我国三大运营商的移动用户数达到13.1亿户,4G /3G用户累计达到8.09亿户,再次创下新高.从三大运营商的年度财报来看,在具体业务方面,三大运营商的数据流量业务带来的收入均已超 ...
- 浅谈Java中的equals和==
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: String str1 = new String("hello"); String str2 = ...
- 玩转Unity资源,对象和序列化(上)
这是一系列文章中的第二章,覆盖了Unity5的Assets,Resources和资源管理 本文将从Unity编辑器和运行时两个角度出发,主要探讨以下两方面内容:Unity序列化系统内部细节以及Unit ...
- magnitude是精确距离,sqrMagnitude是节省CPU的粗略距离,精度有损失
magnitude是精确距离,sqrMagnitude是节省CPU的粗略距离,精度有损失 CubeA坐标 x:0 y:0.844 z:0 CubeC坐标 x:0 y:0 z:0 Vector3 aaa ...
- ASP.NET Boilerplate
I want it to be a start point for all we .NET developers, so, it will be good to develop it together ...
- Manacher's Algorithm 马拉车算法
这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...
- Node+Socketio实现消息群发功能
注:本博文是作者原创,转载请注明出处. 在项目中时常会使用到socketio,今天我们就来实现Node+socketio实现群发消息功能, 项目源码:https://github.com/zhangx ...