写自己的配置文件中的section
来源:互联网 发布:r语言mac版 编辑:程序博客网 时间:2024/05/20 18:02
VS2013 控制台程序
配套代码:http://download.csdn.net/detail/u010476739/9598947
步骤一、确定自己的配置文件最终形式
如:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="jacksection" type="ConsoleApplication2.MySectionHandler,ConsoleApplication2" />
</configSections>
<jacksection>
<MyAdd name="ji" value="de" />
<MyAdd name="ui" value="yh" />
</jacksection>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
步骤二、引用程序集:System.Configuration
步骤三、添加类MySectionSetting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace ConsoleApplication2
{
public class MySectionSetting
{
internal static NameValueCollection props=new NameValueCollection();
public static NameValueCollection Properties
{
get
{
return props;
}
}
}
}
步骤四、添加类MySectionHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;
namespace ConsoleApplication2
{
public class MySectionHandler:IConfigurationSectionHandler
{
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
NameValueCollection kv = new NameValueCollection();
XmlNodeList list= section.SelectNodes("MyAdd");
if (list != null)
{
foreach (XmlNode node in list)
{
XmlAttribute attrName=node.Attributes["name"];
XmlAttribute attrValue=node.Attributes["value"];
if (attrName == null || attrValue == null)
{
continue;
}
kv.Add(attrName.Value, attrValue.Value);
}
}
MySectionSetting.props = kv;
return null;
}
}
}
步骤五、进行测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Specialized;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Test2();
Console.WriteLine("ok");
Console.ReadLine();
}
private static void Test2()
{
ConfigurationManager.GetSection("jacksection");
NameValueCollection kv = MySectionSetting.Properties;
string[] keys=kv.AllKeys;
foreach (var key in keys)
{
Console.WriteLine(key + "--" + kv[key]);
}
}
}
}
步骤六、查看结果
注意:步骤五中的代码:ConfigurationManager.GetSection("jacksection");非常重要,只有调用这个代码,CLR才会去解析
- 写自己的配置文件中的section
- 自己写的log4j配置文件
- C#中读取配置文件ini中的所有Section 以及制定Section下的所有Keys
- 自己写配置文件
- jQuery中的一些自己写的方法
- VC枚举配置文件的Section存入Map
- 编写自己的配置文件
- 自己的vim配置文件
- 自己的~/.bashrc 配置文件
- 自己的vim配置文件
- 自己的VIM配置文件
- 自己写的几个Linux 和 Windows 读ini配置文件的函数
- 自己项目中的proxool和applicationContext.xml写的
- WEB系统启动时加载Log4j的配置文件(自己写Listener形式)
- 写自己的rss
- 写自己的rss
- 自己写的sqlhelper
- 自己写的strcmp
- IOS 监测网络状态
- Spring框架参考文档-核心技术-IoC容器
- Spring Boot实战之配置使用Sentry上报错误日志(一)
- sdut oj3343 数据结构实验之二叉树四:还原二叉树
- 面向对象原则--单一职责原则
- 写自己的配置文件中的section
- LightOJ 1265 Island of Survival(概率DP)
- PHP加密解密明文相同每次密文不一样非对称加解密
- 域操作符::的用法
- http协议学习系列(断点续传和多线程下载的实现原理)
- 一个form表单提交到两个不同的action中
- css3:border-image边框图像详解
- 蓝牙基带 比特流处理
- ajax请求数据,后台提供数据返回json, 前台js拼html,回填