I am opening up an XML file using LINQ-to-XML, but before this I am using OpenMappedExeConfiguration to get the ConnectionStringsSection from the config file:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = exeConfigName;
System.Configuration.Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);`
ConnectionStringsSection section = config.ConnectionStrings;
//Modify connection string...
After I modify part of a ConnectionString, I save the config file via:
config.Save();
After saving the EXE configuration, I open the file again to read some other parts via LINQ-to-XML, but after calling Save() on the document, it appears to not save my ConnectionString changes, but it saves the changes I made with LINQ-to-XML.
XDocument configFile = XDocument.Load(path);
//make changes to configFile
XDocument.Save(path);
I had to move the EXE configuration code below the XDocument code. I can see it changing the ConnectionString in memory before it hits the XDocument code. I am not sure why this is happening, but is it because I have loaded XML into memory and when I save it back, I am essentially overwriting all changes I made with my config.Save()? If this is so, is there a way I can make changes without having to reorder the code?