c# xml.Load() locking file on disk causing errors

泄露秘密 提交于 2019-11-27 21:57:12

it depends on what you need from the file,

If you need it to be threasdsafe you would need to impliment a mutex to lock the loading between instance,

If you dont really need thread safe loading (i.e. the file never changes) you could load it via a filestream then load the XmlDocument from the stream

            FileStream xmlFile = new FileStream(xmlFilePath, FileMode.Open,
FileAccess.Read, FileShare.Read);
            xmlDoc.Load(xmlFile);

You can do this

using (Stream s = File.OpenRead(xmlFilePath))
{
    xmlDoc.Load(s);
}

instead of

xmlDoc.Load(xmlFilePath);
Michael A. McCloskey

If the file isn't too big to read into memory all at once:

xml.Load(new MemoryStream(File.ReadAllBytes(path)));
JeffreyABecker

try:

xml.Load(
       new StreamReader(
           new FileStream(
                  path, 
                  FileMode.Open, 
                  FileAccess.Read, 
                  FileShare.Read)
            )
          );
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!