468,771 Members | 1,859 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to serialize a stream

I have tried both the binary and the XML serialization; both give the
same results.

I have an object with a MemoryStream member. When I serialize and
deserialize the object, the MemoryStream member is created, but it's
actual stream blank. All primitive typed members of my object
deserialize fine.
Can anyone give me a clue about what I need to do to make this happen /
why it's possible / not possible?
Jul 21 '05 #1
2 2496
A memory stream is not a string of bytes. Granted, you can create a memory
stream from a stream of bytes, and in fact, you should be able to read the
string of bytes out of the memory stream and serialize that instead.
However, serializing a stream will not call out all of the data in it.

You may want to make a wrapper object that encapsulates the object in
question. When your wrapper is serialized, it will provide the byte array
for serialization. Deserialization will recreate the memory stream. I
haven't tried this, but, in theory, this may solve the problem.

Sorry I couldn't help more.
--- Nick

"Brad" <bradley_.wood_@ndsu_.nodak_.edu> wrote in message
news:uu**************@TK2MSFTNGP15.phx.gbl...
I have tried both the binary and the XML serialization; both give the
same results.

I have an object with a MemoryStream member. When I serialize and
deserialize the object, the MemoryStream member is created, but it's
actual stream blank. All primitive typed members of my object
deserialize fine.
Can anyone give me a clue about what I need to do to make this happen /
why it's possible / not possible?

Jul 21 '05 #2
Brad wrote:
I have tried both the binary and the XML serialization; both give the
same results.

I have an object with a MemoryStream member. When I serialize and
deserialize the object, the MemoryStream member is created, but it's
actual stream blank. All primitive typed members of my object
deserialize fine.
Can anyone give me a clue about what I need to do to make this happen
/ why it's possible / not possible?


Serializing a stream makes no sense. It is *not* serializable. A stream is
pretty much like the wire that connects your PC to the Internet. Imagine I
wanted a file that you've downloaded from the Internet to your PC. You
wouldn't want me to come to your place and hook my PC up to your wire to get
the file as well -- but that's pretty much what serializing a stream would
mean.

So what you need to do is serialize the actual data, not the medium that
carries it. Since MemoryStream is a simple buffer for bytes, the obvious
solution is to serialize it by calling MemoryStream.ToArray() and serialize
the resulting byte array.

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Carl Gilbert | last post: by
5 posts views Thread by David Sworder | last post: by
5 posts views Thread by andrewcw | last post: by
10 posts views Thread by Dan | last post: by
3 posts views Thread by MAY | last post: by
2 posts views Thread by films | last post: by
5 posts views Thread by Andrew Robinson | last post: by
1 post views Thread by js | last post: by
1 post views Thread by job | last post: by
3 posts views Thread by =?Utf-8?B?UGhpbCBKb2huc29u?= | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.