I have a configuration file, something like:
<logonurls>
<othersettings>
<setting name="DefaultEnv" serializeAs="String">
<value>DEV</value>
</setting>
</othersettings>
<urls>
<setting name="DEV" serializeAs="String">
<value>http://login.dev.server.com/Logon.asmx</value>
</setting>
<setting name="IDE" serializeAs="String">
<value>http://login.ide.server.com/Logon.asmx</value>
</setting>
</urls>
<credentials>
<setting name="LoginUserId" serializeAs="String">
<value>abc</value>
</setting>
<setting name="LoginPassword" serializeAs="String">
<value>123</value>
</setting>
</credentials>
</logonurls>
How can I read configuration to get the value of keyname passed. Here is the method that I wrote:
private static string GetKeyValue(string keyname)
{
string rtnvalue = String.Empty;
try
{
ConfigurationSectionGroup sectionGroup = config.GetSectionGroup("logonurls");
foreach (ConfigurationSection section in sectionGroup.Sections)
{
//I want to loop through all the settings element of the section
}
}
catch (Exception e)
{
}
return rtnvalue;
}
config is the Configuration variable that has the data from the config file.
what about this ? convert it to proper xml and search within the nodes:
just a quick note: if you convert it to xml , you can also use xpath to get the values.
Load your config file into XmlDocument, get XmlElement by name (setting value you want to read) and try following code.
How to write a single LINQ to XML query to iterate through all the child elements & all the attributes of the child elements?