471,616 Members | 1,095 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

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 9484
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 XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.