I have a requirement to create a nested configuration section. The issue is that I need to write an application that accesses any number of databases. These could be oracle, sql, or anything else... I want to make my config section look like this:
<connectionconfigurations>
<databaseConnection dbname="connection1" dbsourceConnect="connectionstring1" provider="sql">
<sqlQueries>
<add name="querynumber1"
sqlFilePath="C:\Users\me\Desktop\sql"/>
<add name="querynumber2"
sqlFilePath="C:\Users\me\Desktop\sql"/>
</sqlQueries>
</databaseConnection>
<databaseConnection dbname="connection1" dbsourceConnect="connectionstring2" provider="oracle">
<sqlQueries>
<add name="querynumber3"
sqlFilePath="C:\Users\me\Desktop\oracle"/>
<add name="querynumber4"
sqlFilePath="C:\Users\me\Desktop\oracle"/>
</sqlQueries>
</databaseConnection>
</connectionconfigurations>
The issue is that I am having trouble accessing all my parameters. How do I create nested config sections like this and access them through code?
I've made a class like this to handle the connection section:
public class Connectionconfigurations : ConfigurationSection
{
private static ConfigurationPropertyCollection _connectionConfiguration;
private static ConfigurationPropertyCollection _sqlQueries;
private static ConfigurationPropertyCollection _properties;
static Connectionconfigurations()
{
_connectionConfiguration = new ConfigurationProperty(
ConfigConstants.CONFIG_DATABASECONNECTION,
typeof(DatabaseConnectionElementCollection),
null,
ConfigurationPropertyOptions.IsRequired);
_sqlQueries = new ConfigurationProperty(
ConfigConstants.CONFIG_SQLQUERIES,
typeof(DatabaseConnectionElementCollection),
null,
ConfigurationPropertyOptions.IsRequired);
_properties = new ConfigurationPropertyCollection();
// Add other properties
_properties.Add(_databaseConnection);
}
[ConfigurationProperty("databaseConnection")]
public DatabaseConnectionElementCollection DatabaseConnection
{
get { return (DatabaseConnectionElementCollection)base[_databaseConnection]; }
}
}
However, I am getting the error: "Unrecognized attribute 'dbname'. Note that attribute names are case-sensitive."
DatabaseConnectionElement class:
public class DatabaseConnectionElement : ConfigurationElement
{
private static ConfigurationProperty _name;
private static ConfigurationProperty _sourceConnect;
private static ConfigurationProperty _provider;
private static SqlQueryElementCollection _sqlCollection; // Collection of sql queries for this DB element
private static ConfigurationPropertyCollection _properties;
static DatabaseConnectionElement()
{
_name = new ConfigurationProperty(ConfigConstants.CONFIG_DB_NAME, typeof(string), string.Empty,
ConfigurationPropertyOptions.IsKey);
_sourceConnect = new ConfigurationProperty(ConfigConstants.CONFIG_DB_SOURCECONNECT, typeof(string), string.Empty,
ConfigurationPropertyOptions.IsRequired);
_provider = new ConfigurationProperty(ConfigConstants.CONFIG_DB_PROVIDER, typeof(string), string.Empty,
ConfigurationPropertyOptions.IsRequired);
_sqlCollection = new SqlQueryElementCollection();
_properties = new ConfigurationPropertyCollection();
_properties.Add(_name);
_properties.Add(_sourceConnect);
_properties.Add(_reconnectDelay);
}
[ConfigurationProperty("dbname", IsKey = true)]
public string Name
{
get
{
return (string)base[_name];
}
}
[ConfigurationProperty("dbsourceConnect", IsRequired = true)]
public string SourceConnect
{
get
{
return (string)base[_sourceConnect];
}
}
[ConfigurationProperty("provider", IsRequired = true)]
public string Provider
{
get
{
return (string)base[_provider];
}
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}
}