tags:

views:

47

answers:

4

Hi,

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????

A: 

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

Xavier Poinas
+3  A: 

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:

http://stackoverflow.com/questions/172646/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!

Kieren Johnstone
I am trying to do somethign like this. vbcity.com/forums/t/161393.aspx , I getting null values
Nimesh
A: 
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;
    }
}
MrFox
I guess this code is to read normal xml file. I have to read web.config using configuration element.
Nimesh
I am trying to do somethign like this.http://vbcity.com/forums/t/161393.aspx , I getting null values.
Nimesh
A: 

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

code4life