使用XMl阅读器读取配置文件

我在web.config文件的AppSettings部分有一堆密钥。 我想使用XML阅读器技术阅读这些应用程序设置的键和值,并在列表框中填充它们。

最好的方法是反转webconfig值是使用System.Configuration.ConfigurationManager.AppSettings;

要从xml reader中检索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]; } 

请享用!