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         }
  • 相关阅读:
    MySQL RR隔离 读一致性
    C++奥赛一本通刷题记录(高精度)
    CodeVs天梯之Diamond
    CodeVs天梯之Gold
    CodeVs天梯之Silver
    CodeVs天梯之Bronze
    【2018.1.14】本蒟蒻又回来了
    test
    UVa12545
    UVa1149
  • 原文地址:https://www.cnblogs.com/lsb123/p/12491743.html
Copyright © 2011-2022 走看看