472,965 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 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 1813
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.