XmlTextReader disposal pattern

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am amazed that XmlTextReader does not implement IDispoable. Is there any
reason?

In the case of XmlTextReader( Stream ) construction, if I wrap the Stream
with a C# using statement like this:
using( StringStream ss = new StringStream.... )
{
XmlTextReader xr = new XmlTextReader( ss );
// ... use xr
}

Will this upset the XmlTextReader's finalizer because that would be executed
non-deterministically long after ss has been disposed?

If inside the using() loop, I called XmlTextReader.Close(), will this upset
the ss.Dispose() call?

Thanks.

Leon
 
Leon said:
Hi,

I am amazed that XmlTextReader does not implement IDispoable. Is
there any reason?

XmlTextReader does implement IDisposable (inherited from XmlReader) in .NET
2.0. Apparently this was overlooked in 1.x.
In the case of XmlTextReader( Stream ) construction, if I wrap the
Stream with a C# using statement like this:
using( StringStream ss = new StringStream.... )
{
XmlTextReader xr = new XmlTextReader( ss );
// ... use xr
}

Will this upset the XmlTextReader's finalizer because that would be
executed non-deterministically long after ss has been disposed?

No. XmlTextReader in 1.0 doesn't have a finalizer (although the VS2003 docs
incorrectly state that it does). In 2.0, the finalizer won't touch the
wrapped stream - it's too late at that point.
If inside the using() loop, I called XmlTextReader.Close(), will this
upset the ss.Dispose() call?

No, the stream will know that it's closed and won't do anything in Dispose.

-cd
 
Back
Top