Friday 15 March 2013

Storing and Retrieving From App.config

 <?xml version="1.0"?>
<configuration>
  <configSections>

    <sectionGroup name="Country">
        <section name="CountryMapping" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>

<connectionStrings>
    <clear />
    <add name="XYZ" connectionString="Data Source=localhost;Initial Catalog=NorthWind; Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

<Country>
    <CountryMapping>

      <add key="United States of America" value="USA"/>
      <add key="Kingdom of Great Britain" value="UK"/>
      <add key="China" value="CH"/>

    </CountryMapping>
  </Country>

....
</configuration>

C# Code:
using System.Collections.Specialized;

 NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("Country/CountryMapping");
foreach (String valueCollection in section)
{
  if (dr["Country"].ToString() == valueCollection)
  {
     dr["Country"] = section[valueCollection];
  }


For more reference:
http://www.codeproject.com/Articles/21036/Working-with-Configuration-Files-app-config-in-C-C

1 comment: