Re-reading a Stream without reopening

T

T Driver

Anyone have any idea how I can do the following?
I have a connection to an XML file on a site I do not control, getting
a string representation of the xml data that I can then feed to my
XmlDataSource object (CurrentXMLData):
Stream newStream =
myWebClient.OpenRead(myStringWebResource);
TextReader newReader = new StreamReader(newStream);
string newData = newReader.ReadToEnd();
CurrentXMLData.Data = newData;

Now, I want to use the stream above as an argument for another method
- but the stream is at the end and doesn't support seeking.
SReader SReader = new SFileReader(newStream); //
EndOfStream is true! so object never initializes.
The SFileReader object will support any inputs that ReadXml on a
dataset will support (XmlReader, TextReader, string filename and
Stream)
The TextReader and XmlReader don't support seeking either.
The only way I've found to do this is to re-open the stream and re-
read - which is very time consuming.

Any ideas on how I can re-use the data in CurrentXMLData, newData or
newStream to populate my SFileReader object?
Thanks in advance,
Ted
 
P

PlatinumBay

T Driver,

You can reset the stream position with the following line of code:

newStream.Seek(0, SeekOrigin.Begin)

Hope this helps,


Steve
 
T

T Driver

T Driver,

You can reset the stream position with the following line of code:

newStream.Seek(0, SeekOrigin.Begin)

Hope this helps,

Steve

"T Driver" <[email protected]> wrote in message


Actually, that was the first thing I tried, but for this stream (it's
not coming from a file) the stream does not support seeking (CanSeek =
false), so this doesn't work.
 
P

PlatinumBay

Ok, fair enough. In that case, MSDN states:

If a class derived from Stream does not support seeking, calls to Length,
SetLength, Position, and Seek throw a NotSupportedException.

You may want to use a different stream reader (such as the MemoryStream)
that supports seeking.

Hope this helps,


Steve
 
T

T Driver

Ok, fair enough. In that case, MSDN states:

If a class derived from Stream does not support seeking, calls to Length,
SetLength, Position, and Seek throw a NotSupportedException.

You may want to use a different stream reader (such as the MemoryStream)
that supports seeking.

Hope this helps,

Steve

Thanks - I thought of those, but hadn't had time to implement yet. I
was hoping there would be something similar to Java's StringStream
class - this would be perfect here
 
P

PlatinumBay

T,

Maybe I should take a step back here, what is it that you are trying to
accomplish?


Steve
 
T

T Driver

T,

Maybe I should take a step back here, what is it that you are trying to
accomplish?

Steve

Good idea, I'll start again, no sense fixing a problem with the path
I'm on if it's the wrong path!
I'm reading XML files that provide status information on a system I'm
working with that get updated periodically on a website I do not
control. I can access the website to see if the file has been
updated, and if it has, I open a stream on it using the above code.
I use the information to populate XmlDataSources that are bound to a
gridView. Additionally, I want to be able to populate a DataSet with
this information that works with another object that does Statistics,
etc., and I'm currently using the ReadXml method on the dataset to
populate it. So I have a stream, and to populate the XmlDataSource, I
create a streamReader and use the ReadToEnd() method to get a string
of data that I then assign to XmlDataSource.Data.
But here I want to rewind the stream (or Seek(0,Begin)), so I can then
populate the Dataset as well using the ReadXml method. ReadXml takes
a stream, a TextReader, an XmlReader or a string defining the file to
open. My ISO won't let me save the data as a file on their server.
Is there a way to do this? (That was my original question.) Or, is
there a way to populate the XmlDataSource with a Dataset? (That's a
new question)
I know I could add all the data to a DB, then requery, but I was
hoping there is a simpler way for this seemingly simple problem.
Thanks for your help,
Ted
 
P

PlatinumBay

Ted,

Ok, what about something like this?

Stream newStream = myWebClient.OpenRead(myStringWebResource);
TextReader newReader = new StreamReader(newStream);
string newData = newReader.ReadToEnd();

XmlDataSource xmld = new XmlDataSource();
xmld.Data = newData;
DataSet ds = new DataSet;
IO.StringReader tr = new IO.StringReader(newData);
ds.ReadXml(tr);
...

(remember to dispose your stream/reader objects)

Hope this helps,


Steve
 
T

T Driver

Ted,

Ok, what about something like this?

Stream newStream = myWebClient.OpenRead(myStringWebResource);
TextReader newReader = new StreamReader(newStream);
string newData = newReader.ReadToEnd();

XmlDataSource xmld = new XmlDataSource();
xmld.Data = newData;
DataSet ds = new DataSet;
IO.StringReader tr = new IO.StringReader(newData);
ds.ReadXml(tr);
...

(remember to dispose your stream/reader objects)

Hope this helps,

Steve

That's it! Exactly what I was looking for - works like a charm.
thanks a million Steve,
Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,821
Messages
2,569,747
Members
45,726
Latest member
RaleighAll

Latest Threads

Top