Question

I am working on an application that reads and makes edits to an xml file but using the XDocument class Load() and Save() methods. If another application, or another instance of my application is running then one document could potentially overwrite changes that the other has made if it's in memory xdocument is not continually updating (reLoading). The simultaneous running programs would never edit the same section of the xml file at the same time. What is the best way to solve this problem? Should I just do a Load right before I make every change or is there a more elegant approach?

Was it helpful?

Solution

The best solution would be not to use XML.

Use a (small) database.

Using any kind of text file in a multi-user situation is difficult enough and the fact that it is to different sections can only be used profitable if you have fixed-length and (therefore) fixed-position records (lines). XML does not deliver this.

Doing a load-before-edit will only make problems appear less often. There always is the chance that changes will be lost, you will have race-conditions at the filesystem level. To make it work you have to use a scheme with locking files. You will need an extra file for this.

OTHER TIPS

You are talking about multiple processes being able to modify the file. If you want to keep the file instead of putting that data into a common store (MS SQL) you will need to implement a mutex to make sure only one application can access file at any moment of time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top