ConfigurationElementCollection KeyValueConfigurationCollection , . , KeyValueConfigurationCollection. , XML :
<providers>
<provider key="products">
<queries>
<add key="whatever" value="stuff" />
...etc...
<queries>
<provider>
</providers>
"", KeyValueConfigurationCollection, "".
Google MSDN, .
-
:
public class ProviderConfiguration : ConfigurationSection
{
[ConfigurationProperty("Providers",IsRequired = true)]
public ProviderElementCollection Providers
{
get{ return (ProviderElementCollection)this["Providers"]; }
set{ this["Providers"] = value; }
}
}
ElementCollection:
public class ProviderCollection : ConfigurationElementCollection
{
public ProviderElement this[object elementKey]
{
get { return BaseGet(elementKey); }
}
public void Add(ProviderElement provider)
{
base.BaseAdd(provider);
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override ConfigurationElement CreateNewElement()
{
return new ProviderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ProviderElement)element).Key;
}
protected override string ElementName
{
get { return "Provider"; }
}
}
Provider:
public class Provider : ConfigurationElement
{
[ConfigurationProperty("Key",IsRequired = true, IsKey = true)]
public string Key
{
get { return (string) this["Key"]; }
set { this["Key"] = value; }
}
[ConfigurationProperty("Queries", IsRequired = true)]
public KeyValueConfigurationCollection Queries
{
get { return (KeyValueConfigurationCollection)this["Queries"]; }
set { this["Queries"] = value; }
}
}
, , KeyValueConfigurationCollection, , , . , , - :
var myConfig = (ProviderConfiguration)ConfigurationManager.GetSection("Providers");
var myValue = myConfig.Providers["Products"].Queries["KeyForKeyValuePairing"].Value;
, . VB:-D