简体   繁体   中英

How to read nested configuration element from the web.config?

I need to read configuration elements from the web.config. Let this be my web.config.

<family>
  <parents>
    <child name="Hello"/>
    <child name="World"/>
  </parents>
 <parents>
    <child name="Hello1"/>
    <child name="World2"/>
  </parents>
</family>

So I have something like this, I need to read this into a collection. How can i do this????

In general, you can store simple application settings and connection string in web.config (or app.config), but anything more complex, like an object graph or XML (as in your case) and you should consider a different method.

These may be helpful:

How do I store an XML value in my .NET App.Config file

(it suggests encoding the XML in an app setting)

However it would be better to have a separate XML data file and convert it to an object graph using Linq-To-XML (see reference ) or XPath and the XmlDocument and related classes.


Edit: see the other answer, which does allow XML in the config file. That's a more direct answer to your exact questions but I will leave this here for reference. On the whole it looks like your data is not configuration data (more like runtime / user data) and does not belong in a .config file: so I would recommend storing it in a separate XML file, and having a config file entry pointing to the filename of the separate XML file.


Hope that helps!

You need to define your own custom configuration section, which will allow you to read the nested configuration element properly. BTW, this is the same method that all the others use, for instance the Enterprise Library components, NHibernate, etc.

The steps you need to take are very straightforward, and a tutorial is provided here:

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

You need to use the ConfigurationElementCollection Class. See this sample on the MSDN

public struct Child
{
    public string name;
    public Child(string name)
    {
        this.name = name;
    }
}

public class Parent
{
    public List<Child> childs = new List<Child>();

    public static List<Parent> ReadParentsFromXml(string fileName)
    {
        List<Parent> parents = new List<Parent>();
        System.Xml.XmlTextReader doc = new System.Xml.XmlTextReader(fileName);
        Parent element = new Parent();

        while (doc.Read())
        {
            switch (doc.Name)
            {
                case "parents":
                    if (doc.NodeType == System.Xml.XmlNodeType.EndElement)
                    {
                        parents.Add(element);
                        element = new Parent();
                    }
                    break;
                case "child":
                    if(doc.NodeType != System.Xml.XmlNodeType.EndElement)
                        element.childs.Add(new Child(doc.GetAttribute(0)));
                    break;
            }
        }

        return parents;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM