How to close instance of an XML DOCUMENT?

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?

  • Answer:

    It depends on which overload of the http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.save.aspx 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); }

gizgok at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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.

driis

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.