Search code examples
c#.netdispose

Does XPathDocument use the memory stream it is constructed with after construction?


I have code like this:

// Take the xml message and turn it into an object
var bytes = Encoding.ASCII.GetBytes(message);
var memoryStream = new MemoryStream(bytes);

XPathDocument xPathDocument = new XPathDocument(memoryStream);

I realized that I don't clean up the MemoryStream anywhere. I was just going to change it to this:

// Take the xml message and turn it into an object
var bytes = Encoding.ASCII.GetBytes(message);
var memoryStream = new MemoryStream(bytes);

XPathDocument xPathDocument;
using(memoryStream)
{
    xPathDocument = new XPathDocument(memoryStream);
}

But I was not sure if XPathDocument uses the MemoryStream internally after construction. (If so, I would need to wait and dispose it after I am all done with the XPathDocument.)

Does anyone know when I can dispose this MemoryStream?


Solution

  • No that's a good change. Once the stream is loaded into the xml, you don't need it any more.

    Well I hope it's good, it's remarkably similar to a lot of the code I've written. :D