I have read a good 15+ articles and tutorials on creating custom configuration sections, and I can't seem to 'get' the custom configuration. ANY help would be appreciated, since I am clearly missing something. It is a simple program to schedule backups and specify a list of source/destination directories.
I am receiving the error: Unrecognized element 'add'. (BattistoneBackup.exe.Config line 8)
My App.config file looks like this:
I am using the following line of code to retrieve the configuration:
Code Copy
BackupDirectorySection oBackupDirectoriesConfigurationSection = BackupDirectorySection.GetConfig();
Here are the classes to wrap the configuration above (all inside the "Battistone.WindowsService.BattistoneBackup" namespace):
C# Copy
public class BackupDirectorySection : ConfigurationSection
{
public static BackupDirectorySection GetConfig ()
{
return ConfigurationManager.GetSection("backupDirectories" ) as BackupDirectorySection;
}
[ConfigurationProperty("backupDirectories" ) ]
public BackupDirectoryConfigurationCollection BackupDirectories
{
get
{
return this ["backupDirectories" ] as BackupDirectoryConfigurationCollection;
}
}
}
public class BackupDirectoryConfigurationCollection : ConfigurationElementCollection
{
public BackupDirectoryConfiguration this [int index]
{
get
{
return base .BaseGet(index) as BackupDirectoryConfiguration;
}
set
{
if (base .BaseGet(index) != null )
{
base .BaseRemoveAt(index);
}
this .BaseAdd(index, value );
}
}
protected override ConfigurationElement CreateNewElement ()
{
return new BackupDirectoryConfiguration();
}
protected override object GetElementKey (ConfigurationElement element )
{
return ((BackupDirectoryConfiguration)element).Name;
}
}
public class BackupDirectoryConfiguration : ConfigurationElement
{
[ConfigurationProperty("Name" ) ]
public string Name
{
get
{
return (string )this ["Name" ];
}
set
{ this ["Name" ] = value ; }
}
[ConfigurationProperty("SourceDirectory" ) ]
public string SourceDirectory
{
get
{
return (string )this ["SourceDirectory" ];
}
set
{ this ["SourceDirectory" ] = value ; }
}
[ConfigurationProperty("DestinationDirectory" ) ]
public string DestinationDirectory
{
get
{
return (string )this ["DestinationDirectory" ];
}
set
{ this ["DestinationDirectory" ] = value ; }
}
}
Amit ChoudharyRank #145 · 15.8K reputationPosted Jan 16, 2011, 6:58 PM EST
Try using XML Namespaces to get rid of this error.
Thanks
mydealbox5 reputationPosted Jan 17, 2011, 12:59 PM EST
Thank you for the help