Why isn't my XML file being saved, even though

2019-08-04 08:41发布

I'm reading the contents of an XML file and parsing that into an object model.

When I modify the values in the object model, then use the following code to save it back to the xml:

XElement optionXml = _panelElement.Elements("options").FirstOrDefault();

optionXml.SetAttributeValue("arming", value.ToString());

_document.Save(_fileName);

This works, as far as I can see, because when I close the application and restart it the values that I had saved are reflected in the object model next time I view it.

However, when I load the actual XML file, the values are still as they were originally.

Why is this? What do I need to do to save the actual XML file with the new values?

3条回答
Luminary・发光体
2楼-- · 2019-08-04 09:09

It turns out the file was being copied to and read from the Output Directory. I can see that it's being updated as expected from there.

查看更多
Explosion°爆炸
3楼-- · 2019-08-04 09:22

You are most likely experiencing file system virtualisation, which was introduced in Windows Vista.

Basically what this means is that you are saving your file, just not where you think you're saving it. For example, you might think that you are saving to C:\Program Files\Your App\yourFile.xml, but what is happening under the hood is that the OS is silently redirecting that to %APPDATA%\Your App\yourFile.xml. When you go to reload it, once again the OS silently redirects from that location.

This is a security measure designed to better encapsulate applications and their data and to prevent unauthorised writes to locations where damage can occur. You can still force a save to %PROGRAMFILES%\Your App, but to do that you either need to relax the ACLs applied to that folder, or you need to elevate the privilege level your application runs at.

查看更多
冷血范
4楼-- · 2019-08-04 09:24

I wasn't sure whether to put this as a comment or as an answer, but I think it could be a potential answer. It sounds like the XML file is being saved because the data is being persisted across instances of the application. It may be file system virtualization like slugster mentioned, but it might be a simple as the fact that you are looking at the wrong copy of the XML file. If you are using a relative path, the file may have been copied to the new location. I would suggest you do a quick file search for that file name and see what you get back.

查看更多
登录 后发表回答