0

I am using following two code to load an XML.Some other process is updating the same XML while i am trying to Load it.It's giving me below error in both case:-

The process cannot access the file 'D:\Projects\WriteOnXml\Xml\test.xml' because it is being used by another process.

1)

using (FileStream xml = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    XDocument xdoc = XDocument.Load(xml);       
}

2)

using (StreamReader sr = new StreamReader(dest))
{
    xdoc = XDocument.Load(sr);
}

I want to load it while it is updating by some other process. Any better approach.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    It's not possible if file is locked by other process. – qxg Nov 02 '15 at 07:21
  • 3
    Even if you can start reading the file via file sharing flags one both sides, surely if one process is in the middle of writing the file, it's unlikely to be valid XML at that point. It would be *far* better to work out some way of not having to do this. – Jon Skeet Nov 02 '15 at 07:21
  • @Oday : - No i don't have any access to code which is updating it. – anup shukla Nov 02 '15 at 07:24

1 Answers1

0

have a look on that question: c# xml.Load() locking file on disk causing errors

it seems that if you could use xml document it will work...

Community
  • 1
  • 1
silver
  • 1,633
  • 1
  • 20
  • 32