所以我在里面使用文件流 xmlreader
using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), readerSettings))
{
reader.close()
}
Run Code Online (Sandbox Code Playgroud)
但是,进入xmlreader的文件仍然处于使用范围之后的锁定状态,很奇怪,我xmlreader
想要为我关闭文件流,不是吗?
感谢帮助.
Eri*_*ick 14
您应该能够通过XmlReaderSettings.CloseInput来控制它.
readerSettings.CloseInput = true;
using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), readerSettings))
{
// do work with the reader
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不关心其他读者设置,则更简洁:
using (XmlReader reader = XmlReader.Create(new FileStream(archivePath, FileMode.Open), new XmlReaderSettings() { CloseInput = true }))
{
// do work with the reader
}
Run Code Online (Sandbox Code Playgroud)
你试过这个吗?
using(var stream = new FileStream(archivePath, FileMode.Open))
using(var reader = XmlReader.Create(stream, readerSettings))
{
}
Run Code Online (Sandbox Code Playgroud)
我在文档中找不到任何明确说明在XmlReader
处理时会在底层流上调用dispose的内容.另外,我总是使用它如上所示,我从来没有遇到过问题.
浏览反射器时我也没有找到Dispose()
创建时在流上调用的实例XmlTextReaderImpl
.在XmlTextReaderImpl
没有实现Dispose()
其Close()
方法如下:
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();
}
}
Run Code Online (Sandbox Code Playgroud)