Mark_Buckingham
asked on
reading .net config files using ConfigurationManager
I am trying to read a bespoke config file "deployment.config" as below.
I am able to open it ok and read the appropriate section into a ConfigurationSection this contains the correct information as proved by writing out the RawXml.
ConfigurationSection does ne no good as it doesn't expose the Settings of the section so i'm assuming i should be using ClientSettingsSection which does.
The problem is if i change line
ConfigurationSection section = config.GetSection("Deploym entConfigu ration");
for
ClientSettingsSection section = config.GetSection("Deploym entConfigu ration") as ClientSettingsSection;
i always get a null result. I would of thought this was a fairly common thing to do am i being stupid ?
I am able to open it ok and read the appropriate section into a ConfigurationSection this contains the correct information as proved by writing out the RawXml.
ConfigurationSection does ne no good as it doesn't expose the Settings of the section so i'm assuming i should be using ClientSettingsSection which does.
The problem is if i change line
ConfigurationSection section = config.GetSection("Deploym
for
ClientSettingsSection section = config.GetSection("Deploym
i always get a null result. I would of thought this was a fairly common thing to do am i being stupid ?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<DeploymentConfiguration>
<add key="MyAppConnectionstring" value="TheConnectionstring"/>
</DeploymentConfiguration>
</configuration>
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Environment.CurrentDirectory + @"\deployment.config";
if (!File.Exists(fileMap.ExeConfigFilename))
{
Console.WriteLine("Configuration file required :" + fileMap.ExeConfigFilename);
return;
}
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection("DeploymentConfiguration");
Console.WriteLine (section.SectionInformation.GetRawXml());
ASKER
Thanks for taking the time to look at this query.
I have a windows app , that currently has some basic config data in the app.config file, this is nice and simple and i can access the key vaule pairs with a simple statement like :-
string LogDirectory = ConfigurationManager.AppSe ttings["Lo gDirectory "];
I want similar ( simple key / value pairs ) but reading from a file that i can name as i please, i.e. "deployment.config".
Sounds like a common thing to do ........ , i can of course read and parse the file as XML but really would have thought that the ability to select a section and read key values pairs would be built into the ConfigurationManager without having to create a class that inherits Configuration etc....
Am i wasting my time looking for something that doesn't exist ?
I have a windows app , that currently has some basic config data in the app.config file, this is nice and simple and i can access the key vaule pairs with a simple statement like :-
string LogDirectory = ConfigurationManager.AppSe
I want similar ( simple key / value pairs ) but reading from a file that i can name as i please, i.e. "deployment.config".
Sounds like a common thing to do ........ , i can of course read and parse the file as XML but really would have thought that the ability to select a section and read key values pairs would be built into the ConfigurationManager without having to create a class that inherits Configuration etc....
Am i wasting my time looking for something that doesn't exist ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok thanks
What aspect of your question could I have provided more info for? I provided examples along with explanations of why your approach was not working and why the examples would work, and would completely resolve your problem, at least as it was stated.
I would like the opportunity to earn an A, what can I provide?
I would like the opportunity to earn an A, what can I provide?
ASKER
Ororiole, thanks for your effortd but i gave a "B" because the config file i was trying to use didn't use the Appsettings section.
<appSettings></appSettings >
but a section called <DeploymentConfiguration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<DeploymentConfiguration>
<add key="MyAppConnectionstring " value="TheConnectionstring "/>
</DeploymentConfiguration>
</configuration>
So i cant actually use your solution but it taught me something i didn't know , the DeploymentConfiguration has been specified by part of the organisation and they are already using this structure via the Spring framework so i can't simply change it.
I now load the settings with the following code instead....
<appSettings></appSettings
but a section called <DeploymentConfiguration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<DeploymentConfiguration>
<add key="MyAppConnectionstring
</DeploymentConfiguration>
</configuration>
So i cant actually use your solution but it taught me something i didn't know , the DeploymentConfiguration has been specified by part of the organisation and they are already using this structure via the Spring framework so i can't simply change it.
I now load the settings with the following code instead....
public bool LoadSettings(string filepath, string section)
{
bool valid = false;
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
try
{
System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filepath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
string Xml = config.GetSection(section).SectionInformation.GetRawXml();
xDoc.LoadXml(Xml);
System.Xml.XmlNode xList = xDoc.ChildNodes[0];
foreach (System.Xml.XmlNode xNode in xList)
{
m_Settings.Add(xNode.Attributes["key"].Value, xNode.Attributes["value"].Value);
}
valid = true;
}
catch (Exception e)
{
Exception = e;
}
return valid;
}
You didnt mention if this was a windows app or a web app. Since you are defining <DeploymentConfiguration> I assume you have a web app. But the <deployment element must come under the <system.web element.
The reason you are getting a null is that a ClientSettingsSection object is not a DeploymentSection. All of them inherit from ConfigurationSection so you can't cast any of the specific Sections to each other.
If you want a custom section, you will have to define one using all this:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx
but if all you want is a connectionString section or maybe you will want to throw in a few other members later then use an appSettings section
Open in new window