Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Re-reading a Stream without reopening

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
T Driver - 05 Jun 2007 17:22 GMT
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
PlatinumBay - 05 Jun 2007 17:47 GMT
T Driver,

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

   newStream.Seek(0, SeekOrigin.Begin)

Hope this helps,

Steve

> 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
[quoted text clipped - 21 lines]
> Thanks in advance,
> Ted
T Driver - 05 Jun 2007 18:03 GMT
> T Driver,
>
[quoted text clipped - 7 lines]
>
> "T Driver" <drive...@gmail.com> 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.
PlatinumBay - 05 Jun 2007 18:13 GMT
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,
>>
[quoted text clipped - 11 lines]
> not coming from a file) the stream does not support seeking (CanSeek =
> false), so this doesn't work.
T Driver - 06 Jun 2007 13:58 GMT
> Ok, fair enough.  In that case, MSDN states:
>
[quoted text clipped - 23 lines]
> > 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
PlatinumBay - 06 Jun 2007 20:03 GMT
T,

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

Steve

> 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
[quoted text clipped - 21 lines]
> Thanks in advance,
> Ted
T Driver - 07 Jun 2007 14:52 GMT
> T,
>
[quoted text clipped - 28 lines]
> > 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
PlatinumBay - 07 Jun 2007 15:16 GMT
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,
>>
[quoted text clipped - 53 lines]
> Thanks for your help,
> Ted
T Driver - 07 Jun 2007 15:49 GMT
> Ted,
>
[quoted text clipped - 16 lines]
>
> Steve

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.