David Ly
David Ly

Reputation: 31596

Does XDocument.Save(string filename) resave the whole file or just changes?

Basically if I do Xdoc.Load(filename), do some changes then do Xdoc.Save(filename) does it only save things that changed such as inserted or removed elements, etc, or does it resave everything?

Depending on the answer I'm thinking of determining whether my app is going to save per-change or save on explicit save and on exit. Also considering whether to write to multiple xml files or just keep everything in one big file. I have no idea how big the one big file would be but I suspect it could potentially be 10's of MBs, so if it's resaving the entire file then I definitely can't be saving every change while keeping one big file.

If it does save the entire file, does anyone have opinions of having a separate xml file for each entity (potentially hundreds) and whether or not it's a good idea?

Upvotes: 1

Views: 3940

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1501626

Yes, saving a document saves the whole document.

What's the use case for the "per change" save? Is it just in case the application crashes? If so, I suggest you save these incremental changes in a temporary directory as small files, but when the user explicitly says to save the file, save it in one big file. (That's easier to copy around etc.) Delete the temporary directory on exit.

I do wonder whether you really need the temporary directory at all though. It sounds like quite a lot of work for little benefit.

Upvotes: 3

Josh
Josh

Reputation: 69252

It saves the whole file. That is the nature of text based formats. A text file cant overwrite itself without rewriting the unchanged parts.

Upvotes: 8

Related Questions