<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<section name="Person" type="System.Configuration.NameValueSectionHandler"/>
<!--以NameValue键值/对的形式返回配置节中的信息-->
<section name="Man" type="System.Configuration.DictionarySectionHandler"/>
<!--以Dictionary字典键值对的形式返回配置节中的信息-->
<section name="Name" type="System.Configuration.SingleTagSectionHandler" />
<!--基础结构。处理 .config 文件中由单个 XML 标记所表示的各配置节。-->
</configSections>
<appSettings>
<add key="Domain" value="xxxxxx"/>
</appSettings>
<connectionStrings>
<add name="ProjectName" connectionString="xxxxxx"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<!---自定义节点-->
<Person>
<add key="zhangsan" value="19931112"/>
<add key="lisi" value="19921111"/>
</Person>
<Man>
<add key="老大" value="f" />
<add key="老二" value="s" />
<add key="老三" value="t" />
</Man>
<Name one="1" two="2" three="3" four="4" five="5" />
<!--注意是要单个节SingleTagSectionHandler才能处理,但是无论你索性有多少个也能处理-->
</configuration>
string text1 = "", text2 = "", text3 = "", text4 = "", text5 = "";
//获取appSettings节点数据
text1 = ConfigurationManager.AppSettings["Domain"];
//获取connectionStrings节点数据
text2 = ConfigurationManager.ConnectionStrings["ProjectName"].ToString();
//获取以NameValue键值/对的形式返回配置节中的信息
NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("Person");
foreach (string key in nvc.AllKeys)
{
text3 += key + ":" + nvc[key];
}
//获取以Dictionary字典键值对的形式返回配置节中的信息
IDictionary dict = (IDictionary)ConfigurationManager.GetSection("Man"); ;
foreach (string key in dict.Keys)
{
text4 += key + ":" + dict[key] + " ";
}
//获取由单个 XML 标记所表示的各配置节
IDictionary dict1 = (IDictionary)ConfigurationManager.GetSection("Name");
foreach (string key in dict1.Keys)
{
text5 += key + ":" + dict1[key];
}
string ret =
string.Format("AppSettings:{0}-ConnectionStrings:{1}-NameValue:{2}-Dictionary:{3}-XML:{4}",
text1, text2, text3, text4, text5);
//"AppSettings:xxxxxx-ConnectionStrings:xxxxxx-NameValue:zhangsan:19931112lisi:19921111-Dictionary:老三:t老大:f老二:s -XML:three:3five:5one:1two:2four:4"