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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Refael AckermannCommented:
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
djcheekyAuthor Commented:
I suspect that the .Read() function doesn't increment the line number, so how would this work then?

Thanks
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Refael AckermannCommented:
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
Refael AckermannCommented:
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
djcheekyAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.