By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,685 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,685 IT Pros & Developers. It's quick & easy.

Re-reading a Stream without reopening

P: n/a
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

Jun 5 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" <dr******@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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

Jun 5 '07 #2

P: n/a
On Jun 5, 10:47 am, "PlatinumBay" <stevan...@community.nospamwrote:
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" <drive...@gmail.comwrote 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.

Jun 5 '07 #3

P: n/a
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 Driver" <dr******@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Jun 5, 10:47 am, "PlatinumBay" <stevan...@community.nospamwrote:
>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" <drive...@gmail.comwrote 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.

Jun 5 '07 #4

P: n/a
On Jun 5, 11:13 am, "PlatinumBay" <stevan...@community.nospamwrote:
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 Driver" <drive...@gmail.comwrote in message

news:11**********************@p77g2000hsh.googlegr oups.com...
On Jun 5, 10:47 am, "PlatinumBay" <stevan...@community.nospamwrote:
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" <drive...@gmail.comwrote 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.
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

Jun 6 '07 #5

P: n/a
T,

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

"T Driver" <dr******@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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

Jun 6 '07 #6

P: n/a
On Jun 6, 1:03 pm, "PlatinumBay" <stevan...@community.nospamwrote:
T,

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

Steve

"T Driver" <drive...@gmail.comwrote in message

news:11**********************@o5g2000hsb.googlegro ups.com...
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
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

Jun 7 '07 #7

P: n/a
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 Driver" <dr******@gmail.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
On Jun 6, 1:03 pm, "PlatinumBay" <stevan...@community.nospamwrote:
>T,

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

Steve

"T Driver" <drive...@gmail.comwrote in message

news:11**********************@o5g2000hsb.googlegr oups.com...
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

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

Jun 7 '07 #8

P: n/a
On Jun 7, 8:16 am, "PlatinumBay" <stevan...@community.nospamwrote:
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

Jun 7 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.