Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Russell Mangel
Guest
 
Posts: n/a
#1: Nov 12 '05
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
}



Christoph Schittko [MVP]
Guest
 
Posts: n/a
#2: Nov 12 '05

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


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" <russell@tymer.net> wrote in message
news:OJAYDPcKEHA.2880@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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
> }
>
>[/color]


Russell Mangel
Guest
 
Posts: n/a
#3: Nov 12 '05

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


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]" <christophdotnetINVALID@austin.rr.com> wrote in
message news:#XNxdXcKEHA.3436@tk2msftngp13.phx.gbl...[color=blue]
> 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" <russell@tymer.net> wrote in message
> news:OJAYDPcKEHA.2880@TK2MSFTNGP10.phx.gbl...[color=green]
> > 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
> > }
> >
> >[/color]
>
>[/color]


Christoph Schittko [MVP]
Guest
 
Posts: n/a
#4: Nov 12 '05

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


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" <russell@tymer.net> wrote in message
news:ueziErcKEHA.556@tk2msftngp13.phx.gbl...[color=blue]
> Aww, bummer, I figured that this was the case, but I wanted to ask[/color]
someone.[color=blue]
>
> I like the using statement.
>
> Thanks for the reply
> Russ
>
> "Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote in
> message news:#XNxdXcKEHA.3436@tk2msftngp13.phx.gbl...[color=green]
> > 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[/color][/color]
interface,[color=blue][color=green]
> > 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" <russell@tymer.net> wrote in message
> > news:OJAYDPcKEHA.2880@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > 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
> > > }
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread