473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 9590
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
4
by: Meir S. | last post by:
I think the following is a bug in XmlTextReader: I need to process large XMLs, that are typically constructed of many small elements nested in the root element. Each inner element represents a...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
1
by: Emsi | last post by:
Hello, how can I read values of child nodes with the XmlTextReader? File format: <root> <items> <item> <field1>value1</field1> <field2>value2</field2>
3
by: prasad | last post by:
Hi, I am using XMLTextReader class to read the xml files. In some cases xml declaration tag might start after space/tab charecters. These kind of files are supported by the browsers and xml dom's...
1
by: Dica | last post by:
hi all first off, i'm not trying to cross post, but couldn't find this newsgroup earlier (got here from a recommendation on microsoft.public.vb, where i originally posted this question). ...
9
by: Jimmy | last post by:
Hello, If I want to check in C# code that if there is a & in my string variable using RegExp, how should I inform RegExp about &-char? Is just &, \&, &amp; or something else? I have strucled with...
3
by: apiringmvp | last post by:
All, So I am creating a function that gets a short blurb of html from a blog. I would like to retain all html formating and images. The code below works well, with the exception of one issue....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.