使用linq to xml修改app.config示例(linq读取xml)
更新时间:2014年02月25日 10:53:45 作者:
这篇文章主要介绍了使用linq to xml修改app.config示例,需要的朋友可以参考下
复制代码 代码如下:
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["节点名称"].Value ="0";
configuration.Save(ConfigurationSaveMode.Modified);
复制代码 代码如下:
//获取config路径
string path = System.Windows.Forms.Application.ExecutablePath + ".config";
XDocument doc = XDocument.Load(path);
//查找所有节点
IEnumerable<XElement> element = doc.Element("configuration").Element("appSettings").Elements();
//遍历节点
foreach (XElement item in element)
{
if (item.Attribute("key") != null && item.Attribute("key").Value == "节点名称")
{
if (item.Attribute("value") != null)
{
item.Attribute("value").SetValue(DateTime.Now.ToString("d"));
}
}
}
//保存
doc.Save(path);
相关文章
c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的区别详解
这篇文章主要介绍了c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的区别,需要的朋友可以参考下2014-07-07C#中JavaScriptSerializer帮助类用法实例
这篇文章主要介绍了C#中JavaScriptSerializer帮助类用法,实例分析了JavaScriptSerializer帮助类处理json字符串时的技巧,需要的朋友可以参考下2014-12-12
最新评论