日期:2014-05-20 浏览次数:20661 次
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="lastid" value="5140"/> </appSettings> </configuration>
var config = System.Configuration. ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); config.AppSettings.Settings.Remove("id"); config.AppSettings.Settings.Add("id", "test"); config.Save(); Console.WriteLine(config.AppSettings.Settings["id"].Value);
------解决方案--------------------
void Main() { var xml=XElement.Load(@"c:\test.xml"); var query=(from k in xml.Descendants("appSettings") where k.Element("add").Attribute("key").Value=="lastid" select k.Element("add")).SingleOrDefault(); if(query !=null) { query.Attribute("value").Value="5555"; xml.Save(@"c:\test.xml"); } }