0

I am having problem reading from App.config.

This is my App.config:

    <?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <configSections>
      <section name="InterestRates_A" type="InterestRates_A_Configuration" />
    </configSections>
      <InterestRates_A>
        <InterestRate_A band="0" validFrom="" validTo="2004-12-31" rate="0.00000"/>
        <InterestRate_A band="1" validFrom="2005-01-01" validTo="2005-12-31" rate="0.04247"/>
        <InterestRate_A band="2" validFrom="2006-01-01" validTo="2006-12-31" rate="0.02986"/>
        <InterestRate_A band="3" validFrom="2007-01-01" validTo="2009-10-30" rate="0.02740"/>
        <InterestRate_A band="4" validFrom="2009-10-31" validTo="" rate="0.02470"/>
      </InterestRates_A>  
</configuration>

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Interest
{
  public class InterestRate_A : ConfigurationElement
  {
    [ConfigurationProperty("band", IsRequired = true)]
    public string Band
    {
      get
      {
        return this["band"] as string;
      }
    }

    [ConfigurationProperty("validFrom", IsRequired = true)]
    public string ValidFrom
    {
      get
      {
        return this["ValidFrom"] as string;
      }
    }

    [ConfigurationProperty("validTo", IsRequired = true)]
    public string ValidTo
    {
      get
      {
        return this["validTo"] as string;
      }
    }

    [ConfigurationProperty("rate", IsRequired = true)]
    public string Rate
    {
      get
      {
        return this["rate"] as string;
      }
    }
  }

  public class InterestRates_A : ConfigurationElementCollection
  {
    public InterestRate_A this[int index]
    {
      get
      {
        return base.BaseGet(index) as InterestRate_A;
      }
      set
      {
        if (base.BaseGet(index) != null)
        {
          base.BaseRemoveAt(index);
        }
        this.BaseAdd(index, value);
      }
    }

    public new InterestRate_A this[string responseString]
    {
      get { return (InterestRate_A)BaseGet(responseString); }
      set
      {
        if (BaseGet(responseString) != null)
        {
          BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
        }
        BaseAdd(value);
      }
    }

    protected override System.Configuration.ConfigurationElement CreateNewElement()
    {
      return new InterestRate_A();
    }

    protected override object GetElementKey(System.Configuration.ConfigurationElement element)
    {
      return ((InterestRate_A)element).Band;
    }
  }

  public class InterestRates_A_Configuration : ConfigurationSection
  {
    public static InterestRates_A_Configuration GetConfig()
    {
      return (InterestRates_A_Configuration)System.Configuration.ConfigurationManager.GetSection("InterestRates_A") ?? new InterestRates_A_Configuration();
    }

    [System.Configuration.ConfigurationProperty("InterestRates_A")]
    [ConfigurationCollection(typeof(InterestRates_A), AddItemName = "InterestRate_A")]
    public InterestRates_A InterestRates_A
    {
      get
      {
        object o = this["InterestRates_A"];
        return o as InterestRates_A;
      }
    }
  }
}

This is how I call it:

  foreach (var item in config.InterestRates_A)
  {

  }

The problem is that it does not find the section at all. What am I missing here? Thank you in advance to all that will take time to help me out!

PS: Once I wrap my hear adound this I would also like to add more configuration sections.

no9
  • 6,424
  • 25
  • 76
  • 115

1 Answers1

0

Without some detailed inspection of your code, looks like your section type is not properly defined, shoud be like type="Interest.InterestRates_A_Configuration"

Btw, it would be best for you to read this in-depth article covering everything that you need to now about .net configuration.

Unraveling the Mysteries of .NET 2.0 Configuration

Nino
  • 6,931
  • 2
  • 27
  • 42