zoukankan      html  css  js  c++  java
  • C#操作XML配置文件

     1          /// <summary>
     2         /// 根据Key取Value值
     3         /// </summary>
     4         /// <param name="key"></param>
    命名空间:

                using System.Configuration;
                using System.Web;

     5         public static string GetValue(string key)
     6         {
     7             return ConfigurationManager.AppSettings[key].ToString().Trim();
     8         }
     9         /// <summary>
    10         /// 根据Key修改Value
    11         /// </summary>
    12         /// <param name="key">要修改的Key</param>
    13         /// <param name="value">要修改为的值</param>
    14         public static void SetValue(string key, string value)
    15         {
    16             System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
    17             xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
    18             System.Xml.XmlNode xNode;
    19             System.Xml.XmlElement xElem1;
    20             System.Xml.XmlElement xElem2;
    21             xNode = xDoc.SelectSingleNode("//appSettings");
    22 
    23             xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
    24             if (xElem1 != null) xElem1.SetAttribute("value", value);
    25             else
    26             {
    27                 xElem2 = xDoc.CreateElement("add");
    28                 xElem2.SetAttribute("key", key);
    29                 xElem2.SetAttribute("value", value);
    30                 xNode.AppendChild(xElem2);
    31             }
    32             xDoc.Save(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
    33         }
  • 相关阅读:
    UPC 5130 Concerts
    poj 1079 Calendar Game
    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛
    CF932E
    浅谈Tarjan算法
    拉格朗日差值
    扩展欧几里得算法(exgcd)
    欧拉定理
    莫比乌斯反演
    除法分块
  • 原文地址:https://www.cnblogs.com/lsb123/p/12491743.html
Copyright © 2011-2022 走看看