469,289 Members | 2,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

XmlTextReader and C# using statement - Does this work okay?

Is it possible to use the using statement with XmlTextReader?
I tryed to use it, but it gives me the error message:
Cannot implicitly convert type 'System.Xml.XmlTextReader' to
'System.IDisposable'

Is there something I am doing wrong?

// This no worky
using(XmlTextReader xtr = new XmlTextReader("C:\\myfile.xml"))
{
// Do something
}
Nov 12 '05 #1
3 9397
No. You're not doing anything wrong.

You can only use the using statement with classes that implement the
IDisposable interface. The XmlTextWriter doesn't implement that interface,
hence the error message.

The work around is to do what the C# complier does for using statement:

XmlTextWriter writer = ...
try
{
// do wtuff with the writer
}
finally
{
writer.Close();
}
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Russell Mangel" <ru*****@tymer.net> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Is it possible to use the using statement with XmlTextReader?
I tryed to use it, but it gives me the error message:
Cannot implicitly convert type 'System.Xml.XmlTextReader' to
'System.IDisposable'

Is there something I am doing wrong?

// This no worky
using(XmlTextReader xtr = new XmlTextReader("C:\\myfile.xml"))
{
// Do something
}

Nov 12 '05 #2
Aww, bummer, I figured that this was the case, but I wanted to ask someone.

I like the using statement.

Thanks for the reply
Russ

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:#X**************@tk2msftngp13.phx.gbl...
No. You're not doing anything wrong.

You can only use the using statement with classes that implement the
IDisposable interface. The XmlTextWriter doesn't implement that interface,
hence the error message.

The work around is to do what the C# complier does for using statement:

XmlTextWriter writer = ...
try
{
// do wtuff with the writer
}
finally
{
writer.Close();
}
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Russell Mangel" <ru*****@tymer.net> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Is it possible to use the using statement with XmlTextReader?
I tryed to use it, but it gives me the error message:
Cannot implicitly convert type 'System.Xml.XmlTextReader' to
'System.IDisposable'

Is there something I am doing wrong?

// This no worky
using(XmlTextReader xtr = new XmlTextReader("C:\\myfile.xml"))
{
// Do something
}


Nov 12 '05 #3
You can write your own class

DisposableXmlTextWriter : XmlTextWriter, IDisposable
{

}

to add IDisposable semantics to the XmlTextWriter

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Russell Mangel" <ru*****@tymer.net> wrote in message
news:ue*************@tk2msftngp13.phx.gbl...
Aww, bummer, I figured that this was the case, but I wanted to ask someone.
I like the using statement.

Thanks for the reply
Russ

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:#X**************@tk2msftngp13.phx.gbl...
No. You're not doing anything wrong.

You can only use the using statement with classes that implement the
IDisposable interface. The XmlTextWriter doesn't implement that interface, hence the error message.

The work around is to do what the C# complier does for using statement:

XmlTextWriter writer = ...
try
{
// do wtuff with the writer
}
finally
{
writer.Close();
}
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Russell Mangel" <ru*****@tymer.net> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Is it possible to use the using statement with XmlTextReader?
I tryed to use it, but it gives me the error message:
Cannot implicitly convert type 'System.Xml.XmlTextReader' to
'System.IDisposable'

Is there something I am doing wrong?

// This no worky
using(XmlTextReader xtr = new XmlTextReader("C:\\myfile.xml"))
{
// Do something
}



Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
4 posts views Thread by Japhy | last post: by
4 posts views Thread by Meir S. | last post: by
4 posts views Thread by Andy Neilson | last post: by
3 posts views Thread by prasad | last post: by
9 posts views Thread by Jimmy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.