【问题标题】:Read config file using XMl reader使用 XML 阅读器读取配置文件
【发布时间】:2010-10-05 22:16:09
【问题描述】:

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

【问题讨论】:

  • 您可以这样做,但简单地使用配置管理器并遍历所有应用程序设置键会更容易。或者使用属性文件并将它们全部放在一个类中。

标签: c# xml web-config drop-down-menu


【解决方案1】:

检索 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

【讨论】:

  • 这种方式比 ConfigurationManager.AppSettings 更好,因为你可以拥有任何你想要的文件名和路径。
【解决方案2】:

您可以获取对 AppSettings NameValueCollection 的引用并按如下方式进行迭代:

NameValueCollection settings = System.Configuration.ConfigurationManager.AppSettings;

foreach (string key in settings.AllKeys)
{
   string value = settings[key];
}

享受吧!

【讨论】:

  • 非常感谢你们的帮助。它确实对我有用。我也想将值保存到配置文件中,无论如何我可以做到这一点
  • 看看这个所以帖子,否则你可以创建一个自定义配置部分并以这种方式保存。 stackoverflow.com/questions/453161/…
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
相关资源
最近更新 更多