I want to create a helper method that can take in ANY type of stream data (from XML, Database, file, etc.) so that we can Convert the stream into a string that is returned.
So my idea is this in pseudo form:
public GetStreamText(Stream stream)
{
string data = string.Empty;
// convert the stream to string and set it to the data variable
return data;
}
The purpose for now to have this helper available is that we’re grabbing HTML content from files. Later on we want this to work for getting that content from the database or other sources. So I’m trying to make this reusable (generic) enough that I don’t have to create a bunch of other methods or redundant code all over the place.
So I could send into this a file, an xml doc, and xml envelope from a response, etc.
I’m not sure if Stream is so generic that every type of object that uses streams inherits form it..but I assume yes.
So I’m not quite sure where to start here on determining how to approach this.
Wouldn’t your purpose be met by the StreamReader class?
Edit: Elaborating…
A stream is essentially an abstracted view of a sequence of bytes. The different types of streams are designed to accommodate different sources of data:
FileStreamreads bytes from a file on disk,MemoryStreamfrom memory,NetworkStreamfrom a network (such as the Internet), and so on. Everything that may be represented as a sequence of bytes may be encapsulated in a stream.Pretty much everything can be represented as a sequence of bytes – that is a premise that underlies all of digital computing – but not everything may be represented as a sequence of textual characters, which is where you need to be careful with your “universal method”. Suppose I give you a stream of image data. You can convert that to a
String, but the content will appear to be garbage because it’s not meant to be interpreted that way.In brief: Yes,
Streamis designed to handle any kind of data.String, on the other hand, is not.