Gantenbein

Hello,

I have a strange problem when using XmlReader/Writer. After writing the xml file and restarting the app I like to read the contents from that file.

Writing the doc
XmlTextWriter writer = null;
try {
XmlDocument doc = new XmlDocument();
writer = new XmlTextWriter(filename, null);
...
doc.Save(writer);
} catch (Exception ex) {
...
} finally {
if (writer != null) writer.Close();
}

... and reading the same file (after restarting the app)
StreamReader sr = null;
try {
XmlDocument doc = new XmlDocument();

// *** THIS LINE CAUSES THE EXCEPTION ***
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs);
doc.LoadXml(sr.ReadToEnd());
...
} catch (Exception ex) {
} finally {
if (sr != null) sr.Close();
}


The line marked above issues the error "The process cannot access the file because another process..." . I tested this also with new files, so I'm pretty sure, that no other app accesses this file.

Must I do something more than just closing the stream
This error is quite annoying, so any help would be appreciated.

Thanks,
Olli


Re: XML and the .NET Framework Strange problem with XmlReader/XmlWriter: "The process cannot access the file '...' because it is being used another process

Paul Louth

You need to dispose your XmlTextWriter, either explicitly:

writer.Dispose();

or by using a 'using' clause:
 
using (XmlTextWriter w = new XmlTextWriter(fileName,null))
{
    doc.Save(writer);
    w.Close();
}





Re: XML and the .NET Framework Strange problem with XmlReader/XmlWriter: "The process cannot access the file '...' because it is being used another process

Mike Danes

"You need to dispose your XmlTextWriter, either explicitly:"

Using using/Dispose would be better indeed but all that Dispose does is to call Close so in this situation the effect will be the same.

Saying that there aren't too much options left:

1) The path to the file in the read case is wrong (altough this would be quite a coincidence to have a path to another file that exists and it's in use)

2) Some other code in the program has opened the file before the code that is presented here.

3) I've seen some cases were this can be cause by an antivirus that scans the file altough this should be quick (provided that the file is not huge :) )





Re: XML and the .NET Framework Strange problem with XmlReader/XmlWriter: "The process cannot access the file '...' because it is being used another process

Gantenbein

Thanks for the quick answer, unfortunately the 'Dispose' method is protected and not to forget the comment provided by Mike

"Error 1 'System.Xml.XmlWriter.Dispose(bool)' is inaccessible due to its protection level ..."

Thank you anyway,
Olli






Re: XML and the .NET Framework Strange problem with XmlReader/XmlWriter: "The process cannot access the file '...' because it is being used another process

Gantenbein

Mike,

1) Very unlikely ;-)
2) This may be a good hint... I will check it, thanks
3) No, the file is just about a few KByte

Olli




Re: XML and the .NET Framework Strange problem with XmlReader/XmlWriter: "The process cannot access the file '...' because it is being used another process

Gantenbein

Mike,

(2) was the error cause, thanks a lot





Re: XML and the .NET Framework Strange problem with XmlReader/XmlWriter: "The process cannot access the file '...' because it is being used another process

Mark Benningfield

Hello All.

Olli:

I don't know if this will be of use or not, as I have never tried it the other way, but I always Flush() a stream before I close it. Is it possible (anyone ) that if the stream still has content and is closed, the lock won't be released

Just a thought.

HTH.






Re: XML and the .NET Framework Strange problem with XmlReader/XmlWriter: "The process cannot access the file '...' because it is being used another process

Mike Danes

Dispose calls Close and Close calls Flush (well, it does not actually call the Flush function but some private methods that does the same). The simplest way to use a stream/writer is with using:

using (TextWriter ....)

{

}

No need for any Dispose,Close,Flush and try/finally blocks