473,320 Members | 1,876 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,320 software developers and data experts.

Buffer bug with XmlTextReader

My scenario:
I'm using an XmlTextReader to Deserialize serveral classes from a single xml
document. Every class i pass the source stream, containing the xml.
Each class subsequently creates an XmlTextReader object with that stream as
input.
However, the second class that performs this operation cannot start reading
at position X, because the first instance of XmlTextReader advanced the
position in the stream to the end of the stream.
This is in contrast with the BinaryReader class, which does not advanced
stream.Position any further then needed.
Is the XmlTextReader class buffering input? This seems to be unwanted
behavior.

Sample:
Here is a sample xml document:
<CLASS1>
<CLASS2>class 1 text</CLASS2>
<CLASS3>class 2 text</CLASS3>
</CLASS1>

Sample implementation in C#:
// image code here to put this xml in a stream object

XmlTextReader reader1 = new XmlTextReader(sourceStream);
reader1.Read()
//at this point, reader1.Name == "CLASS1"

XmlTextReader reader2 = new XmlTextReader(sourceStream);
reader2.Read() // <-- this call failes with the XmlExcpetion 'There is no
root element'. In my opinion the call is valid and reader2.Name should be
"CLASS2"

When I check the value of sourceStream.Position, i conclude that the first
reader has completely read the source stream.

Can I solve this or is it a bug?
Nov 12 '05 #1
3 1834
Kjeld wrote:
I'm using an XmlTextReader to Deserialize serveral classes from a single xml
document. Every class i pass the source stream, containing the xml.
Each class subsequently creates an XmlTextReader object with that stream as
input.


Why don't you pass XmlTextReader instead of bare stream?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #2
Because my object should serialize to XML when debug mode is set in the
app.config (a custom app setting). The object should serialize to a binary
stream when not in debuggin mode (to optimize performance).
All my classes implement I<productname>Serialize which defines a Serialize
and a Deserialize method, taking a System.IO.Stream object as input.

Do you think we are talking about a bug here, or a feature I do not yet know
the reason for? Is there a workaround?
I think I could pass an object to my methods instead of a stream object and
cast the object to whatever writer I'm using. This would solve the
'state/buffer' issue but pollute my interface.

"Oleg Tkachenko [MVP]" schreef:
Kjeld wrote:
I'm using an XmlTextReader to Deserialize serveral classes from a single xml
document. Every class i pass the source stream, containing the xml.
Each class subsequently creates an XmlTextReader object with that stream as
input.


Why don't you pass XmlTextReader instead of bare stream?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #3
Kjeld wrote:
Do you think we are talking about a bug here, or a feature I do not yet know
the reason for? Is there a workaround?


I don't think that's a bug. That's by design.
As a workaround you can have record length or some end-of-record marker
somewhere on the stream and don't let XmlTextReader read more than
needed (either buffer record before parsing - as it's debug anyway or
override Stream to count bytes read).

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #4

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

Similar topics

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...
5
by: Geoff Bennett | last post by:
While parsing an XML document, my TextReader instance skips nodes. For example, in this fragment: <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14"> <RelatedEntityRef...
2
by: Yuriy | last post by:
Hi, any ideas how to read XML fragment from TextReader? XmlTextReader constructor accepts only Stream or string as source Do I miss something? thanks Yuriy
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
by: SHC | last post by:
Hi all, I did the "Build" on the attached code in my VC++ .NET 2003 - Windows XP Pro PC. On the c:\ screen, I got the following: Microsoft Development Environment An unhandled exception of type...
2
by: Q | last post by:
I am feeding XmlTextReader a URL that returns the XML that then gets parsed. The URL forms a query that affects how much data is returned in XML but not the format of the data. The problem is...
2
by: XML reading with XMLTextReader | last post by:
im trying to read an xml file which is in the wwwroot folder.im using IIS on XP Prof. my code is...
4
by: CodeRazor | last post by:
I am reading an xml file from a URL. I was originally doing this using the XmlDocument class. But this was very slow. The XmlTextReader is meant to be much quicker for forward only retrieval of...
4
by: CodeRazor | last post by:
I am trying to use an XmlTextReader to retrieve data. I need to use an XmlTextReader because it is faster than using an XmlDocument. I have found an inelegant way of retrieving each item's title...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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

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.