serialize into stream

A

andrewcw

I have an object to serialize.
[ this works ]
TextWriter writer = new StreamWriter("test.xml");
serializer.Serialize(writer,obj);
writer.Close();

but below does not, why ?? I have a file that I will have
exclusively opened & I use the stream for that operation.
[ error mess Value cannot be null.\r\nParameter name:
stream ] THANKS { I am tring to push the object back into
the stream )

FileStream stream = null;
serializer.Serialize(stream,obj); // CRASH
System.IO.StreamWriter Tout = new System.IO.StreamWriter
(stream);
stream.Position=0;
Tout.Close();
 
J

Jay B. Harlow [MVP - Outlook]

Andrew,
Look carefully at the following two lines:
FileStream stream = null;
serializer.Serialize(stream,obj); // CRASH

The first parameter to Serialize is a null, you are attempting to serialize
the object into nothingness...

You need to change this line:
FileStream stream = null;

To open a new FileStream object. Or if you really do not want it to go
anyplace you can use Stream.Null.
FileStream stream = Stream.Null;
serializer.Serialize(stream,obj);
The object was serialized to 'null'...

Hope this helps
Jay


andrewcw said:
I have an object to serialize.
[ this works ]
TextWriter writer = new StreamWriter("test.xml");
serializer.Serialize(writer,obj);
writer.Close();

but below does not, why ?? I have a file that I will have
exclusively opened & I use the stream for that operation.
[ error mess Value cannot be null.\r\nParameter name:
stream ] THANKS { I am tring to push the object back into
the stream )

FileStream stream = null;
serializer.Serialize(stream,obj); // CRASH
System.IO.StreamWriter Tout = new System.IO.StreamWriter
(stream);
stream.Position=0;
Tout.Close();
 
M

Michael Mayer

Jay B. Harlow said:
To open a new FileStream object. Or if you really do not want it to go
anyplace you can use Stream.Null.

The object was serialized to 'null'...

What on earth is that good for? I guess it'd be like the unix:

cat filename.txt > /dev/null

Sure, it uses up some CPU clocks, but doesn't get much else done ;-)
 
J

Jeffrey Tan[MSFT]

I think this way can let the user determine the appropriate
operation(serialize to a
real stream or just throw it away) during
programming.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Michael Mayer" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: serialize into stream
| Date: Mon, 8 Sep 2003 02:04:31 -0500
| Lines: 23
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: c66.169.139.197.ts46v-08.otn-c2.ftwrth.tx.charter.com
66.169.139.197
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:183101
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
message
| | > To open a new FileStream object. Or if you really do not want it to go
| > anyplace you can use Stream.Null.
| >
| > > FileStream stream = Stream.Null;
| > > serializer.Serialize(stream,obj);
| > The object was serialized to 'null'...
|
| What on earth is that good for? I guess it'd be like the unix:
|
| cat filename.txt > /dev/null
|
| Sure, it uses up some CPU clocks, but doesn't get much else done ;-)
|
|
| --
| Mike Mayer
| http://www.mag37.com/csharp/
| (e-mail address removed)
|
|
|
|
 
J

Jay B. Harlow [MVP - Outlook]

Michael,
I would use it while testing the Serialization code without it going any
place. As Jeffrey stated, I'm sure if Andrew really wanted to copy the file
to dev null, that Andrew knows why he wants to do that. ;-)

Check out the Special Case Pattern:
http://www.martinfowler.com/eaaCatalog/specialCase.html

Useful in places where you use a Null Object (Stream.Null) instead of
checking if a variable is null, do one thing else do the other thing.

Hope this helps
Jay
 
M

Michael Mayer

Jay B. Harlow said:
Michael,
I would use it while testing the Serialization code without it going any
place. As Jeffrey stated, I'm sure if Andrew really wanted to copy the file
to dev null, that Andrew knows why he wants to do that. ;-)

Check out the Special Case Pattern:
http://www.martinfowler.com/eaaCatalog/specialCase.html

Useful in places where you use a Null Object (Stream.Null) instead of
checking if a variable is null, do one thing else do the other thing.

Thanks for that link. I was about to reply to Jeffrey Tan's post saying
that I would just check if the stream was null and then serialize if not
null. Glad I read the above link first. I'll have to keep that in mind as
I write my code, because I'm certainly guilty of the check-for-null
syndrome.

My question in my previous post (why would you want to copy to null) was
certainly more for the OP. But now that I've thought about it a bit more
(and put it in the context of the above article) it certainly makes sense.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top