473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Serialize Object by Object

Hi All,
I am trying to write a simple program which involves serializing objects.
how do I serialize a custom class objects to a single file, I do not want to use a arraylist or a collection , what I am trying to do is create an object when ever user wants and serialize it to a file, All the objects created in this way should be stored in the same file and the user should be able to view all his objects on demand.
I am struck at deserialization, how do i know how many objects are stored in the file. how should I construct the iterating loop?

Thanks in Advance,
Srini.
Feb 28 '08 #1
5 1274
You will somehow need to know the byte length of each object serialized in your file, I am assuming you are using binary serialization. The problem with this is each searialized object won't have the same byte length if you are serializing non-fixed length objects such as strings.

If you want everything in a single file, you can serialize your object into a MemoryStream, figure out the length, write the first 4 bytes in your file as the length of your serialized object, and then write the object. If you do this for each object, write 4 bytes to store the length, then write the bytes for the object. When you desearialize, you will need to read the file into memory, then based on the 4 bytes you wrote to store the length of your object, you can pull out that many bytes and deserialize from a MemoryStream.

What is preventing you from serializing a collection to a single file? That makes more sense and will make your life a lot easier. Just before the user wants to save, store all the objects in a collection, then serialize the collection. Then when you deserialize, just loop through the collection to pull out your objects.


Hope this helps
Feb 28 '08 #2
thankyou for the quick response, Yes what you have said is right, I could use a collection to store the objects and then serialize it or as you have suggested we can do it by writing the block size first and then the block data itself.
But I was just trying to see if I can do that object by object as we do with fwrite ,fread functions In C C++. Apparently I see that there is no way to do that in C#.
Preparing for Application Foudation exam so was Just playing around with C# .

once again thanks for the help.
Feb 28 '08 #3
But lets assume a scenario,
Each time an application runs ,say we want to serialize only a big single object(assume,everytime it will be the same class object that will be serialized)

And in any one the runs ,what if the user wants to see all the objects that were serialized till date ?
yeah one thing can be done if we make a note of number of objects written to disk till date ,we can call deserialize those many number of times and need not bother about the number of bytes (assuming all the objects are of same type)
but I am just trying to think is there any other way to know the number of objects or blocks synchronized.
I wish the deserialize method of binaryformatter had returned true or false if it sees no more bytes in the file.
Please ignore if this does not make sense.
Feb 28 '08 #4
I wish the deserialize method of binaryformatter had returned true or false if it sees no more bytes in the file.
That is the thing with the BinaryFormatter, it reads the entire stream that is provided to the Deserialize function. So you would have to write blocks of bytes from a file to a MemoryStream and pass that to the BinaryFormatter.
Feb 28 '08 #5
Yeah ,you are right,if possible could you post a small code snippet using binaryformatter and memory stream ,that would be great,anyways thanks for sharing your thoughts.

That is the thing with the BinaryFormatter, it reads the entire stream that is provided to the Deserialize function. So you would have to write blocks of bytes from a file to a MemoryStream and pass that to the BinaryFormatter.
Feb 29 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
3
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.