Stack Exchange
ASP.NET MVC Configure the another project webconfig
I want ta change another Webconfig but I'm getting erors
What I tried is
string path = "E:\\username\\myprojects\\myproject\\Web.config";
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(path);
webConfig.AppSettings.Settings.Add("DBNAME", "DEV_TEMP");
webConfig.Save();

This said " Web.config denied relative virtual paths"
and another I tried on webconfig
 <configuration>
   <appSettings>
     <add key="DBNAME" value="DEV_DEVELOPMENT"/>
   </appSettings>

DBNAME value change to "DEV_TEMP"
XDocument doc=XDocument.Load("E:\\username\\myprojects\\myproject\\Web.config");

doc.Element("appSettings").Element("DBNAME").Value = "DEV_TEMP";
doc.Save("Web.config");

and it says Null reference

Close VS before trying to edit a config file.

Assuming you have a good reason to do this, you could use something like:
var addElement = doc.Element("configuration").Element("appSettings").Elements().First(x => x.Attribute("key").Value == "DBNAME");
var valueAttribute = addElement.Attribute("value");
valueAttribute.Value = "DEV_TEMP";

Note that you also need to get the configuration element, and you can get the add element by selecting the element where the key attribute matches DBNAME as there could be multiple add elements. Also value is the attribute of add, not an element itself. See the XML Document Object Model (DOM)
 for further information.
