473,378 Members | 1,152 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,378 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 9594
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.