I have a problem with modifying xml file when i first load and then save it with same file path and name. Below is my code. The error is “Access to the path C:\MyApp\Web.config is denied. If i change the path of the xdoc.Save to be different from xdoc.Load, then it will be ok. What is your recommandation to solve this problem? If possible, i need to modify the existing xml file(meaning xml file for loading and saving is the same path).
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:\\MyApp\\Web.config");
XmlNode xn = xdoc.SelectSingleNode("//configuration/MyProvider");
XmlElement el = (XmlElement)xn;
el.SetAttribute("defaultProvider", "MyCustomValue");
xdoc.Save(@"C:\\MyApp\\Web.config");
Thanks in advance.
I would expect this to be fine if you have write access to
web.configto start with, and if nothing else is using it. (It was certainly fine in a test I just ran.) I suspect it’s more likely that another process is already using the file (or the same process but some other code within it), or that you simply don’t have write access to the file.