XDocument xDocument = XDocument.Load("App.Config");
xDocument.Root.Element("appSettings").Elements("add").ToList().Find(x => x.Attribute("key").Value == "ConnectionString").Attribute("value").Value = "connstr";
xDocument.Save("App.Config");
xDocument.Root.Element("appSettings").Elements("add").ToList().Find(x => x.Attribute("key").Value == "ConnectionString").Attribute("value").Value = "connstr";
xDocument.Save("App.Config");
Configuration configuration = null; //Configuration对象
AppSettingsSection appSection = null; //AppSection对象
configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//取得AppSetting节
appSection = configuration.AppSettings;
//赋值并保存
appSection.Settings["DataSourceConnection"].Value = txtDataSourceConnection.Text;
...
configuration.Save();