title: c# Code to read App.config values
body: <p>I am a newbie to c#, I am having some requirement where i need to read the values of the below App.config file .</p>
  
  <p>Requirement 1.
  In the config node i need to count the no of ArchivePath nodes and loop through
  each of them.Suppose I need to loop through first ArchivePath node and need to access the values of Email ,Name and SSN.Like that I need to loop through all of them.</p>
  
  <p>2.I also need to access the Key LocationName and get the value from the node.</p>
  
  <p>Please iam really not that much expert in c#, Am using .net 4.0</p>
  
  
  
  <pre><code>&lt;configuration&gt;
    &lt;Config&gt;
      &lt;ArchivePath Email="Y" Name="ClientName" SSN="123455678"/&gt;
      &lt;ArchivePath Email="Y" Name="ClientName1" SSN="123455678"/&gt;
      &lt;ArchivePath Email="Y" Name="ClientName2" SSN="123455678"/&gt;
      &lt;/Config&gt;
  
    &lt;appSettings&gt;
      &lt;!--all times in seconds--&gt;
      &lt;add key="LocationName" value="India" /&gt;
  
    &lt;/appSettings&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Thanks in Advance.</p>
  
  <p>I tried below code from the following link
  <a href="http://blog.danskingdom.com/adding-and-accessing-custom-sections-in-your-c-app-config/" rel="nofollow">http://blog.danskingdom.com/adding-and-accessing-custom-sections-in-your-c-app-config/</a></p>
  
  <p>am getting error from below class,
  Is there any thing missing from the code</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace WindowsFormsApplication2
  {
      public class ConnectionManagerDataSection : ConfigurationSection
      {
          /// &lt;summary&gt;
          /// The name of this section in the app.config.
          /// &lt;/summary&gt;
          public const string SectionName = "ConnectionManagerDataSection";
  
          private const string EndpointCollectionName = "ConnectionManagerEndpoints";
  
          [ConfigurationProperty(EndpointCollectionName)]
          [ConfigurationCollection(typeof(ConnectionManagerEndpointsCollection), AddItemName = "add")]
          public ConnectionManagerEndpointsCollection ConnectionManagerEndpoints { get { return (ConnectionManagerEndpointsCollection)base[EndpointCollectionName]; } }
      }
  
      public class ConnectionManagerEndpointsCollection : ConfigurationElementCollection
      {
          protected override ConfigurationElement CreateNewElement()
          {
              return new ConnectionManagerEndpointElement();
          }
  
          protected override object GetElementKey(ConfigurationElement element)
          {
              return ((ConnectionManagerEndpointElement)element).Name;
          }
      }
  
      public class ConnectionManagerEndpointElement : ConfigurationElement
      {
          [ConfigurationProperty("name", IsRequired = true)]
          public string Name
          {
              get { return (string)this["name"]; }
              set { this["name"] = value; }
          }
  
          [ConfigurationProperty("address", IsRequired = true)]
          public string Address
          {
              get { return (string)this["address"]; }
              set { this["address"] = value; }
          }
  
          [ConfigurationProperty("useSSL", IsRequired = false, DefaultValue = false)]
          public bool UseSSL
          {
              get { return (bool)this["useSSL"]; }
              set { this["useSSL"] = value; }
          }
  
          [ConfigurationProperty("securityGroupsAllowedToSaveChanges", IsRequired = false)]
          public string SecurityGroupsAllowedToSaveChanges
          {
              get { return (string)this["securityGroupsAllowedToSaveChanges"]; }
              set { this["securityGroupsAllowedToSaveChanges"] = value; }
          }
  
  
  
  
      }
  }
  var connectionManagerDataSection = ConfigurationManager.GetSection(ConnectionManagerDataSection.SectionName) as ConnectionManagerDataSection;
  if (connectionManagerDataSection != null)
  {
      foreach (ConnectionManagerEndpointElement endpointElement in connectionManagerDataSection.ConnectionManagerEndpoints)
      {
          var endpoint = new ConnectionManagerEndpoint() { Name = endpointElement.Name, ServerInfo = new ConnectionManagerServerInfo() { Address = endpointElement.Address, UseSSL = endpointElement.UseSSL, SecurityGroupsAllowedToSaveChanges = endpointElement.SecurityGroupsAllowedToSaveChanges.Split(',').Where(e =&gt; !string.IsNullOrWhiteSpace(e)).ToList() } };
          AddEndpoint(endpoint);
      }
  }
  </code></pre>