How to parse app.config using ConfigurationManager?
I used a specific method to parse my app.config file. Then I was told that using the ConfigurationManager is better and easier. But the point is, I don't know how to do it using the ConfigurationManager.
My original code looked like this:
XmlNode xmlProvidersNode;
XmlNodeList xmlProvidersList;
XmlNodeList xmlTaskFactoriesList;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("app.config");
xmlProvidersNode = xmlDoc.DocumentElement.SelectSingleNode("TaskProviders");
xmlProvidersList = xmlProvidersNode.SelectNodes("TaskProvider");
foreach (XmlNode xmlProviderElement in xmlProvidersList)
{
if (xmlProviderElement.Attributes.GetNamedItem("Name").Value.Equals(_taskProvider))
{
xmlTaskFactoriesList = xmlProviderElement.SelectNodes("TaskTypeFactory");
foreach (XmlNode xmlTaskFactoryElement in xmlTaskFactoriesList)
{
if (xmlTaskFactoryElement.Attributes.GetNamedItem("TaskType").Value.Equals(_taskType))
{
taskTypeFactory = xmlTaskFactoryElement.Attributes.GetNamedItem("Class").Value;
}
}
}
}
What would be the equivalent using the ConfigurationManager? (Because all I can see is how to get keys, not nodes.)
thanks
a source to share
Create a class that inherits ConfigurationSection
, called, say MyConfigSection
. Then you can use the method ConfigurationManager.GetSection
to get an instance of your class MyConfigSection
. ConfigurationManager
will do all the parsing, so you have a strongly typed object to work with. Here's a great example to follow.
a source to share