Why the filter is not closed by xmlreader

So, I am using a stream in xmlreader

 using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), readerSettings)) { reader.close() } 

However, the file feed in xmlreader is still in a locked state after using the scope, it’s strange, I will close xmlreader to close the stream for me, right?

Thanks for the help.

+6
source share
4 answers

Have you tried this?

 using(var stream = new FileStream(archivePath, FileMode.Open)) using(var reader = XmlReader.Create(stream, readerSettings)) { } 

I could not find anything in the documentation that explicitly stated that XmlReader would call dispose in the underlying thread when it was deleted. In addition, I always use it as shown above, and I have never encountered a problem.

Viewing through a reflector I also do not see instances where it calls Dispose() in the stream when creating an XmlTextReaderImpl . 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(); } } 
+7
source

You should be able to manage this through XmlReaderSettings.CloseInput.

 readerSettings.CloseInput = true; using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), readerSettings)) { // do work with the reader } 

Or, more briefly, if you don't like other reader settings:

 using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), new XmlReaderSettings() { CloseInput = true })) { // do work with the reader } 
+9
source

You will need to track FileStream and XmlReader . It is potentially dangerous for XmlReader to close the main thread. In the case when FileStream used by several readers: if one of these readers was supposed to close the stream, this may lead to unexpected crashes of other readers.

This is a bit of a pain, because some streaming readers and writers close the main stream, while others do not. As a best practice, I always close and manage the flows that I open manually. It also helps soften some "gotchas" with specific threads.
for example, you need to send GZipStream before calling .ToArray()

+1
source

A few years later, but maybe this can help someone ...

I tried Eric's method as it seemed like a good solution, but I kept getting CA2202 warning when I analyzed the VS code on it.

At the bottom of CA2202 , Microsoft recommends that you use the following:

(I changed it a bit for "XmlReader".)

 Stream stream = null; try { stream = new FileStream("file.txt", FileMode.Open); using (XmlReader reader = new XmlReader (stream)) { stream = null; // Use the reader object... } } finally { if(stream != null) stream.Dispose(); } 

instead...

 using (Stream stream = new FileStream("file.txt", FileMode.Open)) { using (XmlReader reader = new XmlReader (stream)) { // Use the reader object... } } 

It is much longer, but at least it does not cause any warnings.

0
source

Source: https://habr.com/ru/post/911530/


All Articles