Dispose or not Dispose.. the writer created from XmlDocument thru navigator?

977 Views Asked by At

Do I really need to dispose the writer below?

DataContractSerializer _serialier...
var actual = new XmlDocument();
using (var writer = actual.CreateNavigator().AppendChild())
    _serialier.WriteObject(writer, myObj);

If not then the code is simplified to:

DataContractSerializer _serialier...
var actual = new XmlDocument();
_serialier.WriteObject(actual.CreateNavigator().AppendChild(), myObj);
1

There are 1 best solutions below

4
On BEST ANSWER

If the object implements IDisposable, then you should call Dispose on it when you're done.

If you don't do that, then your code is dependent on the assumption that you don't need to do it. What happens when your code is later refactored such that the XmlWriter being used is one that holds on to some resource?