源码github地址: https://github.com/yangwohenmai/TEST/tree/master/SetConfiguration
在配置文件app.config中,想要自定义节点的话,格式还是很重要的
1.
首先要创建一个<configSections>节点,而且这个节点要在<configuration>节点内部的最顶端,否则会报错“无法初始化”。
再把后面想定义的节点结构写在<configSections>节点中,这就好像是先在<configSections>写里了一个目录一样。
格式就是:
<configuration>
<configSections>
<sectionGroup name="TestGroup" >
<section name="Test" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
</configuration>
以上就是一个最简单的“目录”结构,标红的地方就是下面要定义的节点结构
2.
定义好目录后,就开始定义节点内容了,就是一般的xml结构内容
如下:
<TestGroup>
<Test>
<add key="Hello" value="World"/>
<add key="Hello1" value="World1"/>
<add key="Hello2" value="World2"/>
<add key="Hello3" value="World3"/>
</Test>
</TestGroup>
标红的地方就是和上面“目录”中对应的节点。这就是一个完整的对应结构了。
3.
下面给出一个样例代码供参考
首先定义一个多节点的app.config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="TestGroup" >
<section name="Test" type="System.Configuration.NameValueSectionHandler"/>
<section name="Test1" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
<sectionGroup name="newGroup" >
<section name="new" type="System.Configuration.NameValueSectionHandler"/>
<section name="new1" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<TestGroup>
<Test>
<add key="Hello" value="World"/>
<add key="Hello1" value="World1"/>
<add key="Hello2" value="World2"/>
<add key="Hello3" value="World3"/>
</Test>
<Test1>
<add key="let's" value="go"/>
<add key="let's1" value="go1"/>
<add key="let's2" value="go2"/>
<add key="let's3" value="go3"/>
</Test1>
</TestGroup>
<newGroup>
<new>
<add key="happy" value="day"/>
<add key="happy1" value="day1"/>
<add key="happy2" value="day2"/>
<add key="happy3" value="day3"/>
</new>
<new1>
<add key="good" value="man"/>
<add key="good1" value="man1"/>
<add key="good2" value="man2"/>
<add key="good3" value="man3"/>
</new1>
</newGroup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
4.
再定义一个输出节点内容的方法
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SetConfiguration
{
class Program
{
static void Main(string[] args)
{
NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test");
NameValueCollection nc1 = (NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test1");
NameValueCollection nc0 = (NameValueCollection)ConfigurationSettings.GetConfig("TestGroup");
for (int i = 0; i < nc.AllKeys.Length; i++ )
Console.WriteLine(nc.AllKeys[i].ToString() + " " + nc[nc.AllKeys[i].ToString()]);
for (int i = 0; i < nc1.AllKeys.Length; i++)
Console.WriteLine(nc1.AllKeys[i].ToString() + " " + nc1[nc1.AllKeys[i].ToString()]);
NameValueCollection neww = (NameValueCollection)ConfigurationSettings.GetConfig("newGroup/new");
NameValueCollection neww1 = (NameValueCollection)ConfigurationSettings.GetConfig("newGroup/new1");
for (int i = 0; i < neww.AllKeys.Length; i++)
Console.WriteLine(neww.AllKeys[i].ToString() + " " + neww[neww.AllKeys[i].ToString()]);
for (int i = 0; i < neww1.AllKeys.Length; i++)
Console.WriteLine(neww1.AllKeys[i].ToString() + " " + neww1[neww1.AllKeys[i].ToString()]);
Console.ReadLine();
}
}
}
这种写法有助于我们清晰的定义出配置文件的结构,不至于满篇全是add出来的Key-Values,不但看着乱,还会被领导同事吐槽。而且自己获取的时候直接循环遍历就行,放在程序初始化中加载成对象,也方便后面使用
源码见文首的github地址。