One example of custom section in configuration file.
App.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="links" type="System.Configuration.NameValueSectionHandler">
</section>
</configSections>
<links>
<add key="link1" value="http://www.milosev.com/csharp/" />
<add key="link2" value="http://milosev.com/asp-net-mvc-3" />
</links>
<appSettings>
<add key="homePage" value="http://milosev.com/" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
.NET code:
using System;
using System.Collections.Specialized;
using System.Configuration;
namespace CustomConfig
{
class Program
{
static void Main(string[] args)
{
NameValueCollection links = ConfigurationManager.GetSection("links") as NameValueCollection;
string homePage = ConfigurationManager.AppSettings.Get("homePage");
Console.WriteLine("Home page: " + homePage);
foreach (string link in links)
{
Console.WriteLine("Link: " + links.Get(link));
}
Console.WriteLine("Press any key...");
Console.ReadKey();
}
}
}
In reference list you will need to add System.Configuration.
POI:
<configSections>
<section name="links" type="System.Configuration.NameValueSectionHandler">
</section>
</configSections>
Here is more about configSection.
Also notice line:
Console.WriteLine("Link: " +
links.Get(link));