Reputation: 5385
I need to store my application config settings in a hierarchical format in web.config or app.config. Is this possible? or I have to store it in some XML file or database and use it instead? plain name value pair format isn't enough for me.
<appSettings>
<Report1>
<add key="SourceFilePath" value="value1" />
<add key="DestinationFolderPath" value="value2" />
</Report1>
<Report2>
<add key="SourceFilePath" value="value1" />
<add key="DestinationFolderPath" value="value2" />
</Report2>
</appSettings>
It is a web-based reporting application and I need to store the file paths for the source files, SSIS packages, FTP server details etc.
Update: If I choose the custom config section option, can I store the settings in a separate file to keep the main web.config file clean from app settings?
Upvotes: 9
Views: 10412
Reputation: 77
You can't store xml elements they way you suggested in appsettings. If you need a hierarchy as you mentioned try one of the following approaches
Store whole xml elements in appsettings an parse the xml
<appSettings>
<add key="Report1" value="<Report1 SourceFilePath=""value1"" DestinationFolderPath=""value2"" />" />
<add key="Report2" value="<Report2 SourceFilePath=""value1"" DestinationFolderPath=""value2"" />" />
</appSettings>
Store the xml in config section (see answers above)
Use the DslConfig project.
Add it with NuGet and look add the AppSpike for examples.
In your case you could create a class ReportConfig and use this class as configuration class.
Example:
public class Report {
string SourceFilePath { get; set; }
string DesinationFolderPath { get; set; }
}
Create a config File entry in Variables.var
import ReportClassNamespace from ReportClassAssembly
Var["Report1"] = Report(SourceFilePath:"value1",DesinationFolderPath:"value2")
Var["Report2"] = Report(SourceFilePath:"value1",DesinationFolderPath:"value2")
Upvotes: 3
Reputation: 37633
The best approach I know is to create your own Settings class and serialize/desiarilze it in order to read/write your app. settings.
Also you can use Admin. page of your website to save all settings into it.
May be in your case is better to use a database to keep all settings there as well.
Upvotes: 0
Reputation: 30666
You can't do that as you suggest.
What you can do is to group by the key name the elements:
<appSettings>
<add key="Report1:SourceFilePath" value="value1" />
<add key="Report1:DestinationFolderPath" value="value2" />
<add key="Report2:SourceFilePath" value="value1" />
<add key="Report2:DestinationFolderPath" value="value2" />
</appSettings>
The best way though would be to define your own ConfigurationSection.
Here's some links about this:
Upvotes: 3
Reputation: 499002
You can't add custom elements to appSettings
.
The best way to achieve a custom format it to write your own ConfigurationSettings
class and use that in configuration.
This will allow you to store data in a strongly typed way as well as have meaningful element and attribute names.
See How to: Create Custom Configuration Sections Using ConfigurationSection on MSDN.
Upvotes: 6