简体   繁体   English

如何在xml文件中分开读写?

[英]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? 读取和写入XML文件的最佳方法是什么,以便我的写入不会停止读取? 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. 目前我正在使用XDocument.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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