473,516 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLReader Problem

Ben
Hi,

I'm just moving to VB.NET and I'm trying to load a recordset intoan XMLReader and loop through the records. When I use the.ReadElementString() method to get the next 'record' if it hasreached the last record (eof) it fails with "Invalid attempt toread when reader is closed". It doesn't seem capable of findingthis out until the error has occured though (Readstate isinteractive prior to the failure and EOF is false.)

Is this limited information enough for anyone to tell me whereI'm going wrong ? (currently as I'm in the early stages I haveif err.number = 5 then err.clear which is a bit crap so ...)

Thanks,

Ben.
Nov 12 '05 #1
11 3713
How are you looping through the reader?

Are you checking the return value from the call to the Read method?

While reader.Read()

' do stuff ...

End While

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
"Ben" <gr*********@hotmail.com> wrote in message
news:Os*************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm just moving to VB.NET and I'm trying to load a recordset into an
XMLReader and loop through the records. When I use the .ReadElementString()
method to get the next 'record' if it has reached the last record (eof) it
fails with "Invalid attempt to read when reader is closed". It doesn't seem
capable of finding this out until the error has occured though (Readstate is
interactive prior to the failure and EOF is false.)

Is this limited information enough for anyone to tell me where I'm going
wrong ? (currently as I'm in the early stages I have if err.number = 5 then
err.clear which is a bit crap so ...)

Thanks,

Ben.
Nov 12 '05 #2
Hi Ben,
Even I am facing the Same Problem. I am getting three records without Root
Element. I am able to read the values to the End After the End It saying the
same error like "Invalid attempt to read when reader is closed". Any thing
we are doing wrong. Please reply me if you have found out the answer.

Thanks
Anandraj.A.

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:#v**************@tk2msftngp13.phx.gbl...
How are you looping through the reader?

Are you checking the return value from the call to the Read method?

While reader.Read()

' do stuff ...

End While

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
"Ben" <gr*********@hotmail.com> wrote in message
news:Os*************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm just moving to VB.NET and I'm trying to load a recordset into an
XMLReader and loop through the records. When I use the ..ReadElementString() method to get the next 'record' if it has reached the last record (eof) it
fails with "Invalid attempt to read when reader is closed". It doesn't seem capable of finding this out until the error has occured though (Readstate is interactive prior to the failure and EOF is false.)

Is this limited information enough for anyone to tell me where I'm going
wrong ? (currently as I'm in the early stages I have if err.number = 5 then err.clear which is a bit crap so ...)

Thanks,

Ben.

Nov 12 '05 #3
Anandraj wrote:
Even I am facing the Same Problem. I am getting three records without Root
Element. I am able to read the values to the End After the End It saying the
same error like "Invalid attempt to read when reader is closed". Any thing
we are doing wrong. Please reply me if you have found out the answer.


It's not clear what do you mean. Show us some code.
Basically it's your responsibility not to call XmlReader's methods when
there is no more input to read.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Nov 12 '05 #4
Hi Oleg,
Here is the detailed description.
I am using only one method to access the Database where the output is XML. I
want to to return the XML to the Client Component. so my code will go like
this below.
XmlReader XmlRdr=null;

... SQL Statement

XmlRdr=Cmd.ExecuteXmlReader();

....

return XmlRdr;

I have read we have to close the XMLReader Otherwise it will create problem.

http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconObtainingDataAsXMLFromSQLServer.asp

So Where I have to close the Reader ? I am calling this Method in my Client
Component. I am getting the Exception only in the client Component.

If I put the same code in the Component where the Database Accessing is
Happpening it is working fine.

Please give Suggestions

Thanks

Anandraj.A.

"Oleg Tkachenko" <oleg@no_!spam!_please!tkachenko.com> wrote in message
news:uc**************@TK2MSFTNGP11.phx.gbl...
Anandraj wrote:
Even I am facing the Same Problem. I am getting three records without Root Element. I am able to read the values to the End After the End It saying the same error like "Invalid attempt to read when reader is closed". Any thing we are doing wrong. Please reply me if you have found out the answer.


It's not clear what do you mean. Show us some code.
Basically it's your responsibility not to call XmlReader's methods when
there is no more input to read.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 12 '05 #5
Anandraj wrote:
So Where I have to close the Reader ? I am calling this Method in my Client
Component. I am getting the Exception only in the client Component.


So am I right that you pass XmlReader client side, read XML from it
successfully and then having exception on Close() method? What's the
exception exactly looks like (along with stack trace, please)?

--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Nov 12 '05 #6
Hi Oleg,
My Component Code goes like htis.
public class WM_Helper

{

public WM_Helper() {}

public static XmlReader GetXMLSet(string SPName)

{

// Creating Connection and Command

XmlReader XmlRdr=null;

string ConString=ConfigurationSettings.AppSettings["ConnectionString"];

SqlConnection Con=new SqlConnection(ConString);

SqlCommand Cmd=new SqlCommand(SPName,Con);

Cmd.CommandType=CommandType.StoredProcedure;

try

{

Con.Open();

XmlRdr=Cmd.ExecuteXmlReader();

}

catch

{

throw new Exception(" Error while executing " + SPName);

}

finally

{

//Disposing the Objects

Con.Dispose();

Cmd.Parameters.Clear();

Cmd.Dispose();

}

return XmlRdr;

}

}

My client code is like this

//try

//{

XmlReader red=WM_Helper.GetXMLSet("GetStatusList");

while(red.Read())

{

}

//}

//catch(Exception Ex)

//{

// string str=Ex.Message;

//}

Exception is "Invalid attempt to Read when reader is closed."

Stack Trace is
Invalid attempt to Read when reader is closed.
at System.Data.SqlClient.SqlDataReader.Read()
at System.Data.SqlClient.SqlStream.Read(Byte[] buffer, Int32 offset, Int32
count)
at System.Xml.XmlStreamReader.Read(Byte[] data, Int32 offset, Int32
length)
at System.Xml.XmlScanner.Read()
at System.Xml.XmlScanner.ScanContent()
at System.Xml.XmlTextReader.ParseBeginTagExpandCharEn tities()
at System.Xml.XmlTextReader.Read()
at MDB.WebForm1.Button7_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\mdb\webform1.aspx.cs:line 175
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePo
stBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
at System.Web.UI.Page.ProcessRequestMain()

Hope this will help you.

Thanks

Anandraj.A.

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:Ox**************@TK2MSFTNGP10.phx.gbl...
Anandraj wrote:
So Where I have to close the Reader ? I am calling this Method in my Client Component. I am getting the Exception only in the client Component.


So am I right that you pass XmlReader client side, read XML from it
successfully and then having exception on Close() method? What's the
exception exactly looks like (along with stack trace, please)?

--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com

Nov 12 '05 #7
Anandraj wrote:

Con.Open();

XmlRdr=Cmd.ExecuteXmlReader();

}

catch

{

throw new Exception(" Error while executing " + SPName);

}

finally

{

//Disposing the Objects

Con.Dispose();

Cmd.Parameters.Clear();

Cmd.Dispose();


This looks suspicious to me. You are creating XmlReader, but closing
connection and command, which provides data XmlReader is supposed to read.
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com
Nov 12 '05 #8
Actually I want to write a Common Component for Where Input is Empty and
Output is XML.Usually for Master Datas.
So How can we do this? Here the SP will return the XMLReader as Output from
there I have to get the Different XML in diffrent Client and then I have to
Parse and Show it to the User.
Any Idea to Implement will be help for me.

Thanks
Anandraj.A.

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
Anandraj wrote:

Con.Open();

XmlRdr=Cmd.ExecuteXmlReader();

}

catch

{

throw new Exception(" Error while executing " + SPName);

}

finally

{

//Disposing the Objects

Con.Dispose();

Cmd.Parameters.Clear();

Cmd.Dispose();


This looks suspicious to me. You are creating XmlReader, but closing
connection and command, which provides data XmlReader is supposed to read.
--
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com

Nov 12 '05 #9
why r u not thinking of DATASET ??
"Anandraj" <a_*************@yahoo.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
Actually I want to write a Common Component for Where Input is Empty and
Output is XML.Usually for Master Datas.
So How can we do this? Here the SP will return the XMLReader as Output from there I have to get the Different XML in diffrent Client and then I have to Parse and Show it to the User.
Any Idea to Implement will be help for me.

Thanks
Anandraj.A.

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
Anandraj wrote:

Con.Open();

XmlRdr=Cmd.ExecuteXmlReader();

}

catch

{

throw new Exception(" Error while executing " + SPName);

}

finally

{

//Disposing the Objects

Con.Dispose();

Cmd.Parameters.Clear();

Cmd.Dispose();


This looks suspicious to me. You are creating XmlReader, but closing
connection and command, which provides data XmlReader is supposed to read. --
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com


Nov 12 '05 #10
Hi Jack,
We are not using the Dataset at all. We are store the data in the Objects
and Sending to the clinet similar to Javabeans in Java.
Is there any way to do that using the XMLReader. It will help ful to know
the reason and Answer for the Question.
anand
"Jack" <ja**@somewhere.com> wrote in message
news:#m**************@TK2MSFTNGP09.phx.gbl...
why r u not thinking of DATASET ??
"Anandraj" <a_*************@yahoo.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
Actually I want to write a Common Component for Where Input is Empty and
Output is XML.Usually for Master Datas.
So How can we do this? Here the SP will return the XMLReader as Output

from
there I have to get the Different XML in diffrent Client and then I have

to
Parse and Show it to the User.
Any Idea to Implement will be help for me.

Thanks
Anandraj.A.

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
Anandraj wrote:
> Con.Open();
>
> XmlRdr=Cmd.ExecuteXmlReader();
>
> }
>
> catch
>
> {
>
> throw new Exception(" Error while executing " + SPName);
>
> }
>
> finally
>
> {
>
> //Disposing the Objects
>
> Con.Dispose();
>
> Cmd.Parameters.Clear();
>
> Cmd.Dispose();

This looks suspicious to me. You are creating XmlReader, but closing
connection and command, which provides data XmlReader is supposed to read. --
Oleg Tkachenko
XmlInsider
http://blog.tkachenko.com



Nov 12 '05 #11
is this win app????


"Anandraj" <a_*************@yahoo.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
Hi Jack,
We are not using the Dataset at all. We are store the data in the Objects
and Sending to the clinet similar to Javabeans in Java.
Is there any way to do that using the XMLReader. It will help ful to know
the reason and Answer for the Question.
anand
"Jack" <ja**@somewhere.com> wrote in message
news:#m**************@TK2MSFTNGP09.phx.gbl...
why r u not thinking of DATASET ??
"Anandraj" <a_*************@yahoo.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
Actually I want to write a Common Component for Where Input is Empty and Output is XML.Usually for Master Datas.
So How can we do this? Here the SP will return the XMLReader as Output

from
there I have to get the Different XML in diffrent Client and then I
have to
Parse and Show it to the User.
Any Idea to Implement will be help for me.

Thanks
Anandraj.A.

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
> Anandraj wrote:
>
>
> > Con.Open();
> >
> > XmlRdr=Cmd.ExecuteXmlReader();
> >
> > }
> >
> > catch
> >
> > {
> >
> > throw new Exception(" Error while executing " + SPName);
> >
> > }
> >
> > finally
> >
> > {
> >
> > //Disposing the Objects
> >
> > Con.Dispose();
> >
> > Cmd.Parameters.Clear();
> >
> > Cmd.Dispose();
>
> This looks suspicious to me. You are creating XmlReader, but closing
> connection and command, which provides data XmlReader is supposed to

read.
> --
> Oleg Tkachenko
> XmlInsider
> http://blog.tkachenko.com



Nov 12 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5407
by: CGuy | last post by:
Hi, I am using an XmlTextReader to read an xml file. It may happen that the file is present in the disk, but it may be empty (0 bytes). I would like to find out whether the xml file contains a valid root node or not. How do I do this? This is what I need if(File.Exists(fileName)) {
5
5624
by: xmlguy | last post by:
I believe this should be pretty elementary, but for some reason I cannot seem to think of how to write the an XML file from an incoming XML file. Basically this is what I do: Input: XmlReader instance from another module (beyond my control)
2
3702
by: xmlguy | last post by:
Cant seem to solve this problem I need to be able to re-use XmlReader and XPathDocument for an XSLT Transform. Basically I have defined following interfaces: Class Render (Common and public inside the class)
1
2297
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code: Public Class Form1 Inherits System.Windows.Forms.Form
0
2624
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader, reader2, after creating new XmlReaderSettings and a new XmlParserContext. // customContext and customSettings may or may not make use reader1's...
2
6822
by: jonfroehlich | last post by:
According to the MSDN documentation within the XmlTextReader class for ..NET 2.0, the recommended practice to create XmlReader instances is using the XmlReaderSettings class and the XmlReader.Create() method. However, the problem is, the XmlReader class does not expose certain properties that I need, e.g., LineNumber, LinePosition, etc. I...
2
7549
by: raj | last post by:
i have this code which is upgraded to work with 2.0 and works great with framework 1.0 and 1.1 without the upgraded code. //2.0 code// public static XmlReader RtfToXml( string Rtf ) { if ( moForm == null )
1
3461
by: Franck | last post by:
Hi, I'm quit confused with the following problem: I'm tryin to read an Embedded Xml file from my Class Library using a static method which should returns a XmlReader. The point is that using DataSet or XmlDocument works perfectly... using XmlReader always fails. I always get "None" as a result. Even using XmlReaderSettings does not...
1
4371
by: dignan.tenenbaum | last post by:
Hello, I'm using the XmlReader.ReadOuterXml() method to return the string representation of an xml node. The XmlReader is created with a file path and a XmlReaderSettings object. This was working fine until a default namespace was introduced. When the default namespace is defined the ReadOuterXml() method began adding a bunch of extra...
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.