How to dispose or close an instance of XML Document in ASP.NET
There's a problem, wherein a XML Document.Save is resulting in the error the process cannot access the file, because it is being used by another process or the statement an Invalid XML Document.I think it's because I do not dispose the XML Document object开发者_如何转开发 after it's operation is complete.Is it possible to do this.Is there a workaround?
It depends on which overload of the Save method you are using. 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:
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.
Perhaps you are using an underlying Stream
or similar to read the documents, and that is what needs to be closed ? Without more context, it is almost impossible to answer.
精彩评论