Loop through the configrationsection to read it

2019-04-03 05:16发布

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.

2条回答
beautiful°
2楼-- · 2019-04-03 05:31

what about this ? convert it to proper xml and search within the nodes:

        private static string GetKeyValue(string keyname) {     
        string rtnvalue = String.Empty;     
        try     {
            ConfigurationSectionGroup sectionGroup = config.GetSectionGroup("logonurls");
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(sectionGroup);

            foreach (System.Xml.XmlNode node in doc.ChildNodes)         
            {             
                //I want to loop through all the settings element of the section         
                Console.WriteLine(node.Value);
            }     
        }     
        catch (Exception e)     
        {     
        }     return rtnvalue; 
    } 

just a quick note: if you convert it to xml , you can also use xpath to get the values.

System.Xml.XmlNode element = doc.SelectSingleNode("/NODE");
查看更多
ら.Afraid
3楼-- · 2019-04-03 05:42

Load your config file into XmlDocument, get XmlElement by name (setting value you want to read) and try following code.

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xmlfilename);

XmlElement elem = doc.GetElementByName("keyname");
var allDescendants = myElement.DescendantsAndSelf();
var allDescendantsWithAttributes = allDescendants.SelectMany(elem =>
    new[] { elem }.Concat(elem.Attributes().Cast<XContainer>()));

foreach (XContainer elementOrAttribute in allDescendantsWithAttributes)
{
    // ...
}

How to write a single LINQ to XML query to iterate through all the child elements & all the attributes of the child elements?

查看更多
登录 后发表回答