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?