469,327 Members | 1,277 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

String stream in .NET???

Hey there,

I need a string stream, but I can't find one in .NET. I thought StringWriter
would derive from Stream, alas it doesn't do so.

Which leads me to my next question: What is the purpose of
System.IO.StringWriter (or, what is its added value)??? It can do the same
things with StringBuilder, and I see no added value.

The reason why I need the string stream, is that I have a library that knows
how to serialize certain library objects to a System.IO.Stream. My intent is
to serialize to a string, but I cannot do that with the current interface.

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder..., not any preallocated byte buffer.

What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad.

Any comment is welcome.

Thanks,
--
Tom Tempelaere.
Nov 16 '05 #1
4 26242
Tom,
Which leads me to my next question: What is the purpose of
System.IO.StringWriter (or, what is its added value)??? StringWriter inherits from TextWriter, it can be used any place a TextWriter
can be used. Creating functions that expect a TextWriter allows you to write
to either a Stream or a String...

The reason why I need the string stream, is that I have a library that
knows
how to serialize certain library objects to a System.IO.Stream. My intent
is
to serialize to a string, but I cannot do that with the current interface. Remember that a String is a series of Unicode Chars (16 bit values). While a
Stream is a series of Bytes (8 bit values).
What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad. You could define one, however I wonder how well the encoding
(System.Text.Encoding) would work, as a series of Bytes is not necessarily
in any specific encoding, in fact a series of Bytes may be in multiple
encodings!

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder..., not any preallocated byte buffer. A Memory stream does not need to be preallocated, it can dynamically grow
just as a StringBuilder.
I would recommend using the MemoryStream when I wanted to serialize to
memory...

Hope this helps
Jay

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:E7**********************************@microsof t.com... Hey there,

I need a string stream, but I can't find one in .NET. I thought
StringWriter
would derive from Stream, alas it doesn't do so.

Which leads me to my next question: What is the purpose of
System.IO.StringWriter (or, what is its added value)??? It can do the same
things with StringBuilder, and I see no added value.

The reason why I need the string stream, is that I have a library that
knows
how to serialize certain library objects to a System.IO.Stream. My intent
is
to serialize to a string, but I cannot do that with the current interface.

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder..., not any preallocated byte buffer.

What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad.

Any comment is welcome.

Thanks,
--
Tom Tempelaere.

Nov 16 '05 #2
Hi Jay,

Thanks for the response.

What should I do to get a string if I would serialize to a MemoryStream?
I've tried:

string s = encoding.GetString( ms.GetBuffer(), 0, (int) ms.Length );

but all that comes out is crap. I've tried ASCIIEncoding and UTF8 and UTF7.
I guess there is no good way...

Thanks,
Tom.
Nov 16 '05 #3
Tom,
but all that comes out is crap. That's what I would expect you will see. As serialization to a stream is
more then likely Binary serialization. Binary serialization has no really
textual representation.

TextWriter (along with its descendents StringWriter & StreamWriter) are used
to create textual representation of objects. If you are writing to a Stream
this "Textual Representation" cannot magically happen by simply using a
StringWriter...
What should I do to get a string if I would serialize to a MemoryStream?
What are you expecting the string to look like? A String is text that people
can read, a Stream is bytes that people cannot read (well people can read
them, but they tend to have all sorts of funny characters in random order).

Hope this helps
Jay

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:87**********************************@microsof t.com... Hi Jay,

Thanks for the response.

What should I do to get a string if I would serialize to a MemoryStream?
I've tried:

string s = encoding.GetString( ms.GetBuffer(), 0, (int) ms.Length );

but all that comes out is crap. I've tried ASCIIEncoding and UTF8 and
UTF7.
I guess there is no good way...

Thanks,
Tom.

Nov 16 '05 #4
Jay,

Thank you for sharing your insight. I now have a clearer view. The
serialization methods in the library were indeed for binary serialization and
not for string serialization.

I had to write my own string serialization routine.

Perhaps I made a mistake in comparing the use of std::stringstream in C++ to
streams in general.

Thank you,
Tom Tempelaere.

"Jay B. Harlow [MVP - Outlook]" wrote:
Tom,
but all that comes out is crap.

That's what I would expect you will see. As serialization to a stream is
more then likely Binary serialization. Binary serialization has no really
textual representation.

TextWriter (along with its descendents StringWriter & StreamWriter) are used
to create textual representation of objects. If you are writing to a Stream
this "Textual Representation" cannot magically happen by simply using a
StringWriter...
What should I do to get a string if I would serialize to a MemoryStream?


What are you expecting the string to look like? A String is text that people
can read, a Stream is bytes that people cannot read (well people can read
them, but they tend to have all sorts of funny characters in random order).

Hope this helps
Jay

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:87**********************************@microsof t.com...
Hi Jay,

Thanks for the response.

What should I do to get a string if I would serialize to a MemoryStream?
I've tried:

string s = encoding.GetString( ms.GetBuffer(), 0, (int) ms.Length );

but all that comes out is crap. I've tried ASCIIEncoding and UTF8 and
UTF7.
I guess there is no good way...

Thanks,
Tom.


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Der Andere | last post: by
2 posts views Thread by JM | last post: by
4 posts views Thread by dave | last post: by
14 posts views Thread by msnews.microsoft.com | last post: by
3 posts views Thread by Bob Altman | last post: by
7 posts views Thread by Lee | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.