Copying an XMLTextReader Object

Hi

I have two XML reader objects, readerA and readerB which read the same XML.

I then perform a number of read operations on readerA, which moves readerA to a different position to readerB.

Now, I want to set readerB's position to the same position as that of readerA.

But if I do the following:
readerA = readerB;

I seem to do a shallow copy of readerA and everytime I do a read on readerA thereafter, readerB reads as well.

How do I do a proper copy of readerB from readerA, without having readerB read everytime readerA does??

Thanks

djcheekyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
djcheekyConnect With a Mentor Author Commented:
Ok - I found a way how to accomplish what I was looking for. It builds on your idea of creating a new XmlTextReader from the position of another.

What I did was create XmlTextReader C, and while C's.Position is less than the original XmlTextReaders position, then I just keep reading within TextReader C. Eventually it lands up at the same position as the original :)

Thanks for the help
0
 
moseackCommented:
Simplest approach is to use XmlReader.Create with the appropriate XmlReaderSettings (line number and position offsets).
0
 
djcheekyAuthor Commented:
Thanks - that seems like a good way to do it but the line number position and offset values always seem to be 0??? Even after numerous .Read() operations??
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
djcheekyAuthor Commented:
I suspect that the .Read() function doesn't increment the line number, so how would this work then?

Thanks
0
 
moseackCommented:
For this to work the undelining stream need to have lines (i.e. a file):
http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.linenumber.aspx

Can you provide more information about the use case, and maybe we can find a different solution for this problem?
0
 
djcheekyAuthor Commented:
Sure - what I am doing is creating two xmlReaders, readerA and readerB.

Due to the fact that these xmlReaders can't move backwards, I use readerB to read forward and search for a particular element. If I find this element, I want to reset readerB's position to that of readerA's.

So my thought was to recreate readerB with xmlReaderSettings lineNumber set to that of readerA.

I do not want to use xmlDocument due to the performance implications either.

You see where I'm coming from?
Thanks
0
 
moseackConnect With a Mentor Commented:
You could use XmlTextReader.GetRemainder to extract the end of the document from readerA, and recreate two new readers from the returned stream.
0
 
djcheekyAuthor Commented:
The problem is that these are XmlReaders, not XMLTextReaders.

I could change them to XmlTextReader, but then I lose the ability to set the "ConformanceLevel = Fragment" setting of the XmlReader, which is critical .

What I also can't understand why you can't assign XMLReaderSettings to an XmlTextReader when XmlTextReader inherits from XmlReader??

0
All Courses

From novice to tech pro — start learning today.