Search code examples
.netxmlidisposableusingxmldocument

XMLDocument disposal - why doesnt it support IDisposable?


The XMLDocument class would seem like an ideal candidate for supporting IDisposable because...

  • it could potentially hold a lot of data.
  • its a represents a possibly complex model of data.

This would allow you to use it inside a Using { ... } statement and it would be garbage collected immediately after use.

However it doesnt appear to support it.

In that case whats the best way to dispose of it?

Or doesnt it need to support IDisposable - I suppose you can just set its reference to null when you have finished with it?

Or is the key difference here that it doesnt tie up external resources such as DB connections or external files and hence doesnt require IDispoable "support"?


Solution

  • Or is the key difference here that it doesnt tie up external resources such as DB connections or external files and hence doesnt require IDispoable "support"?

    Yup, you've hit the nail on the head there. The only resource it uses is memory (granted potentially lots of objects to represent an XML document), and we've a perfectly cromulent facility for managing memory in .net. i.e. the garbage collector.