. NET configuration file Common configuration description
Last Update:2017-02-28
Source: Internet
Author: User
The contents of the configuration file are as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<section name= "MySection" type= "System.Configuration.NameValueSectionHandler"/>
<section name= "mysingletagsection" type= "System.Configuration.SingleTagSectionHandler"/>
<section name= "mydictionarysection" type= "System.Configuration.DictionarySectionHandler"/>
<sectiongroup name= "Mysections" >
<section name= "MySection1" type= "System.Configuration.NameValueSectionHandler"/>
<section name= "MySection2" type= "System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<mySection>
<add key= "Key1" value= "value1"/>
<add key= "Key2" value= "value2"/>
</mySection>
<mysingletagsection setting1= "value1" setting2= "value2" setting3= "Value3"/>
<myDictionarySection>
<add key= "DictionarySectionKey1" value= "DictionarySectionValue1"/>
<add key= "DictionarySectionKey2" value= "DictionarySectionValue2"/>
<add key= "DictionarySectionKey3" value= "DictionarySectionValue3"/>
</myDictionarySection>
<mySections>
<mySection1>
<add key= "Mysection1_key1" value= "mysection1_value1"/>
<add key= "Mysection1_key2" value= "Mysection1_value2"/>
</mySection1>
<mySection2>
<add key= "Mysection2_key1" value= "mysection2_value1"/>
<add key= "Mysection2_key2" value= "Mysection2_value2"/>
</mySection2>
</mySections>
<appSettings>
<add key= "Appkey1" value= "appvalue1"/>
<add key= "Appkey2" value= "Appvalue2"/>
</appSettings>
</configuration>
The code snippet is as follows:
private static void Testappsettings ()
{
Console.WriteLine ("Testappsettings ...");
NameValueCollection config = configurationsettings.appsettings;
Console.WriteLine ("Value1:" + config["Key1"]);
Console.WriteLine ("value2:" + config["Key2"]);
Console.WriteLine ();
}
private static void Testsection ()
{
Console.WriteLine ("Testsection ...");
NameValueCollection config;
Config = (NameValueCollection) configurationsettings.getconfig ("MySection");
Console.WriteLine ("Value1:" + config["Key1"]);
Console.WriteLine ("value2:" + config["Key2"]);
Console.WriteLine ();
}
private static void Testsingletagsection ()
{
Console.WriteLine ("Testsingletagsection ...");
IDictionary dic;
DIC = (IDictionary) configurationsettings.getconfig ("Mysingletagsection");
Console.WriteLine ("Value1:" + dic["setting1"]);
Console.WriteLine ("value2:" + dic["setting2"]);
Console.WriteLine ("Value3:" + dic["Setting3"]);
Console.WriteLine ();
}
private static void Testdictionarysection ()
{
Console.WriteLine ("Testdictionarysection ...");
IDictionary dic;
DIC = (IDictionary) configurationsettings.getconfig ("Mydictionarysection");
Console.WriteLine ("DictionarySectionValue1:" + dic["DictionarySectionKey1"]);
Console.WriteLine ("DictionarySectionValue2:" + dic["DictionarySectionKey2"]);
Console.WriteLine ("DictionarySectionValue3:" + dic["DictionarySectionKey3"]);
Console.WriteLine ();
}
private static void Testsectiongroup ()
{
Console.WriteLine ("Testsectiongroup ...");
NameValueCollection config1;
NameValueCollection Config2;
CONFIG1 = (NameValueCollection) configurationsettings.getconfig ("Mysections/mysection1");
Config2 = (NameValueCollection) configurationsettings.getconfig ("Mysections/mysection2");
Console.WriteLine ("Mysections/mysections1/value1:" + config1["mysection1_key1"]);
Console.WriteLine ("Mysections/mysections1/value2:" + config1["Mysection1_key2"]);
Console.WriteLine ("Mysections/mysections2/value1:" + config2["mysection2_key1"]);
Console.WriteLine ("Mysections/mysections2/value2:" + config2["Mysection2_key2"]);
Console.WriteLine ();
}