开发者

How to set the path of Xml file in Web.config and bind the data from Xml into drop down

I have a Xml file with values for the dropdown.I want to provide the path in Web.config and bind the values 开发者_JAVA技巧to drop down from web.config.


Firstly to read the location from web.config use System.Configuration class, something like the following should work

string filePath = ConfigurationManager.AppSettings["FilePath"];

to access a file on the server use Server.MapPath e.g.

Server.MapPath(filepath);

to bind an xml file to the dropdown you could use the following, there are easier ways but this will allow for any other manipulation you need to do

1: Get the list of items

public static List<string> GetFamiliesList()
    {
        List<string> families = new List<string>();
        try
        {
            using (StreamReader streamreader = new StreamReader(Server.MapPath(filepath)))
            {
                XElement xe = XElement.Load(streamreader);
                foreach (XElement children in xe.Elements("Family"))
                {
                    families.Add(children.Attribute("Name").Value);
                }
            }
        }
        catch
        {

        }
        return families;
    }

2: bind to dropdown

 dropdownList.DataSource = GetFamiliesList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜