简体   繁体   中英

How to separate reading and writing in an xml file?

What is the best way to read and write to an XML file so my writes to not stop my reads? What I would like is two separate classes doing this that are independent and hence to not share locks/mutexes etc.

Currently I am using XDocument.Load() to read the file which I believe takes a lock on the file only on the load.

I would not generally recommend both read/write at the same time but assuming you know the risks, the read lock can be removed if you first load the file into memory:

byte[] buffer = File.ReadAllBytes("myFile");
var ms = new MemoryStream(buffer);
var doc = XDocument.Load(ms);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM