【发布时间】:2010-10-05 22:16:09
【问题描述】:
我的 web.config 文件的 AppSettings 部分中有一堆键。我想使用 XML 阅读器技术读取这些应用程序设置的键和值,并将它们填充到列表框中。
【问题讨论】:
-
您可以这样做,但简单地使用配置管理器并遍历所有应用程序设置键会更容易。或者使用属性文件并将它们全部放在一个类中。
标签: c# xml web-config drop-down-menu
我的 web.config 文件的 AppSettings 部分中有一堆键。我想使用 XML 阅读器技术读取这些应用程序设置的键和值,并将它们填充到列表框中。
【问题讨论】:
标签: c# xml web-config drop-down-menu
检索 webconfig 值的最佳方法是使用 System.Configuration.ConfigurationManager.AppSettings;
从 xml 阅读器中检索 webconfig 的值:
private void loadConfig()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load( Server.MapPath("~/") + "web.config");
XmlNode xnodes = xdoc.SelectSingleNode ("/configuration/appSettings");
foreach (XmlNode xnn in xnodes .ChildNodes)
{
ListBox1.Items.Add(xnn.Attributes[0].Value + " = " + xnn.Attributes[1].Value );
}
}
参考:http://dotnetacademy.blogspot.com/2010/10/read-config-file-using-xml-reader.html
【讨论】:
您可以获取对 AppSettings NameValueCollection 的引用并按如下方式进行迭代:
NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;
foreach (string key in settings.AllKeys)
{
string value = settings[key];
}
享受吧!
【讨论】: