So I am using the FileStream
inside XmlReader
using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), readerSettings))
{
reader.close()
}
However, the file feed into the XmlReader
is still in the lock state after the using
scope, weird, I thought the XmlReader
is going to close the FileStream
for me, is it not?
Thanks for help.
Have you tried this?
using(var stream = new FileStream(archivePath, FileMode.Open))
using(var reader = XmlReader.Create(stream, readerSettings))
{
}
I couldn't find anything in the documentation that explicitly stated that the XmlReader
would call dispose on the underlying stream when it was disposed. Also, I always use it as shown above and I have never encountered a problem.
Browsing through reflector I also find no instances where it calls Dispose()
on the stream when creating a XmlTextReaderImpl
. The XmlTextReaderImpl
does not implement Dispose()
and its Close()
method looks like this:
internal void Close(bool closeInput)
{
if (this.parsingFunction != ParsingFunction.ReaderClosed)
{
while (this.InEntity)
{
this.PopParsingState();
}
this.ps.Close(closeInput);
this.curNode = NodeData.None;
this.parsingFunction = ParsingFunction.ReaderClosed;
this.reportedEncoding = null;
this.reportedBaseUri = string.Empty;
this.readState = ReadState.Closed;
this.fullAttrCleanup = false;
this.ResetAttributes();
}
}