2016-11-29 7 views
0

J'ai problème la lecture de App.config.lecture de la configuration personnalisée dans App.config

Ceci est mon 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> 

Voici mon 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; 
     } 
    } 
    } 
} 

Voici comment je l'appelle:

foreach (var item in config.InterestRates_A) 
    { 

    } 

Le problème est qu'il ne pas trouvé la section du tout. Qu'est-ce que j'oublie ici? Nous vous remercions à l'avance pour tout ce qui va prendre du temps pour me aider!

PS: Une fois que je conclurai mon entendre adound cela, je voudrais également ajouter d'autres sections de configuration.

Répondre

0

Sans une inspection détaillée de votre code, ressemble à votre type de section n'est pas correctement définie, qui devraient être comme type="Interest.InterestRates_A_Configuration"

BTW, il serait préférable pour vous de lire cet article couvrant tout en profondeur que vous avez besoin à propos de la configuration .net.

Unraveling the Mysteries of .NET 2.0 Configuration