0

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.

4

1 回答 1

2

如果您一开始就具有写访问权限web.config,并且没有其他人在使用它,我希望这会很好。(在我刚刚运行的测试中肯定没问题。)我怀疑另一个进程很可能已经在使用该文件(或相同的进程,但其中包含一些其他代码),或者您根本没有写入权限文件。

于 2011-11-22T09:13:19.623 回答