Link to home
Start Free TrialLog in
Avatar of djcheeky
djcheekyFlag for United States of America

asked on

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

Avatar of Refael Ackermann
Refael Ackermann
Flag of United States of America image

Simplest approach is to use XmlReader.Create with the appropriate XmlReaderSettings (line number and position offsets).
Avatar of djcheeky

ASKER

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

Thanks
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?
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
SOLUTION
Avatar of Refael Ackermann
Refael Ackermann
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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??

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial