How to modify by loading and saving the same xml file
- by cynthia hong
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.