Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copying an XMLTextReader Object

Posted on 2008-11-03
8
Medium Priority
?
1,166 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:djcheeky
  • 5
  • 3
8 Comments
 
LVL 7

Expert Comment

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

Author Comment

by:djcheeky
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??
0
 

Author Comment

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

Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:moseack
ID: 22869431
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
 

Author Comment

by:djcheeky
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?
Thanks
0
 
LVL 7

Assisted Solution

by:moseack
moseack earned 1000 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.
0
 

Author Comment

by:djcheeky
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??

0
 

Accepted Solution

by:
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
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

972 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