动态修改配置文件节点

View Code
 ///<summary>
/// 更新某个节点中的值
///</summary>
private void updata()
{
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

foreach (XmlElement element in xmlDoc.DocumentElement)
{
if (element.Name.Equals("appSettings"))
{
foreach (XmlNode node in element.ChildNodes)
{
if (node.Attributes[0].Value.Equals("t5"))
{
node.Attributes[1].Value = "aaaa";
}
}
}
}
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("appSettings"); //强制加载新的 配置文件
}
View Code
 1  ///<summary>
2 /// 查看
3 ///</summary>
4 private void showconfig()
5 {
6 foreach (string key in ConfigurationManager.AppSettings) //遍历得到所有 appsetting 节点
7 {
8 string value = ConfigurationManager.AppSettings[key];
9 Response.Write(string.Format("key:{0},value:{1}", key, value)+"<br>");
10 }
11
12 }




作者:javaoraspx
出处:http://www.cnblogs.com/xyong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/xyong/p/2260471.html