简体   繁体   English

如何在ASP.NET中处置或关闭XML Document的实例

[英]How to dispose or close an instance of XML Document in ASP.NET

有一个问题,其中XML Document.Save导致进程无法访问该文件的错误,因为它正被另一个进程或语句使用无效的XML文档。我认为这是因为我没有处置XML Document对象在它的操作完成后。是否可以这样做。有解决方法吗?

It depends on which overload of the Save method you are using. 这取决于您正在使用的Save方法的重载。 If you pass directly a filename as string there shouldn't be issues. 如果直接传递文件名作为字符串,则应该没有问题。 If you pass a stream or xmlwriter you need to ensure that it is properly disposed: 如果您传递流或xmlwriter,您需要确保它已正确处理:

using (Stream stream = ...)
{
    doc.Save(stream);
}

XmlDocument, as well as the newer XDocument, are in-memory representations of an XML document, so you don't need to close them. XmlDocument以及较新的XDocument是XML文档的内存表示,因此您无需关闭它们。

Perhaps you are using an underlying Stream or similar to read the documents, and that is what needs to be closed ? 也许你正在使用底层Stream或类似的东西来阅读文档,那是什么需要关闭? Without more context, it is almost impossible to answer. 没有更多的背景,几乎不可能回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM