How to delete an instance of an XML document in ASP.NET or close it

There is a problem in which the XML Document.Save leads to an error, the process cannot access the file because it is being used by another process or the Invalid XML Document.I operator I think because I do not dispose of the XML Document Object after the operation is completed. Is it possible to do this. Is there a workaround?

+4
source share
2 answers

It depends on which overload of the Save method you are using. If you pass the file name directly as a string, there should be no problem. If you are passing a stream or xmlwriter, you need to make sure that it is positioned correctly:

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

XmlDocument, like the new XDocument, are representations of an XML document in memory, so you do not need to close them.

Perhaps you are using a basic Stream or similar to reading documents, and this is what you need to close? Without additional context it is almost impossible to answer.

+3
source

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


All Articles