473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Re-reading a Stream without reopening

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
8 3734
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
3
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.