is it necessary to close, dispose or otherwise close an XML document after loading it as an Xelement?

Dim xmldoc As XElement = XElement.Load(xPath)

im transitioning from using streamreader and ini files into using XML and im not sure if I need to worry about freeing up resources etc..


1 Year
Discussion Span
Last Post by cgeier

Do I need to dispose/close XDocument.Load(string)?

"No, you don't - it doesn't even implement IDisposable. The XDocument and XElement classes use XmlReader under the covers and handle the disposing of the underlying reader for you."

Using Statement (Visual Basic)

"...Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need a Using block for managed resources. However, you can still use a Using block to force the disposal of a managed resource instead of waiting for the garbage collector.

A Using block has three parts: acquisition, usage, and disposal.

Acquisition means creating a variable and initializing it to refer to the system resource. The Using statement can acquire one or more resources, or you can acquire exactly one resource before entering the block and supply it to the Using statement. If you supply resourceexpression, you must acquire the resource before passing control to the Using statement.

Usage means accessing the resources and performing actions with them. The statements between Using and End Using represent the usage of the resources.

Disposal means calling the Dispose method on the object in resourcename. This allows the object to cleanly terminate its resources. The End Using statement disposes of the resources under the Using block's control..."

Edited by cgeier

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.