Copying an XMLTextReader Object

Posted on 2008-11-03
Last Modified: 2012-05-05

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??


Question by:djcheeky
  • 5
  • 3

Expert Comment

ID: 22865800
Simplest approach is to use XmlReader.Create with the appropriate XmlReaderSettings (line number and position offsets).

Author Comment

ID: 22866385
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??

Author Comment

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


Expert Comment

ID: 22869431
For this to work the undelining stream need to have lines (i.e. a file):

Can you provide more information about the use case, and maybe we can find a different solution for this problem?
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.


Author Comment

ID: 22869848
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?

Assisted Solution

moseack earned 250 total points
ID: 22873837
You could use XmlTextReader.GetRemainder to extract the end of the document from readerA, and recreate two new readers from the returned stream.

Author Comment

ID: 22874228
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??


Accepted Solution

djcheeky earned 0 total points
ID: 22884461
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now