473,386 Members | 1,598 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,386 software developers and data experts.

Serializing Multiple Objects of a Class

Hi!

I have a class named CClassToSerialize that contain some boolean, string and
few custom variable types.

Now when I create only a single Object and Serialize/Deserialize everything
is fine.

But I want to save more than one Object of this class into same file.

Now the problem is that if I create first object and Serialize it after
initializing some variables. Size of file is 905 bytes. Again when I create
another object of the same class and Serialize it after Initializing same
variables but with different values the size of the file becomes 1,799
bytes. which is not double of 905.

Now I dont know how to Deserialize each object independently.

So anybody there to help me how to achieve this task.

Thanks in Advance

Ahmad Jalil Qarshi
Jan 25 '06 #1
5 13388
[Serializable]
public class SampleClass
{
private int _int1;
private int _int2;
private bool _bool;
private string _string;

public SampleClass(int i1, int i2, bool b, string s)
{
_int1 = i1;
_int2 = i2;
_bool = b;
_string = s;
}

public string StringValue
{
get { return _string; }
}

public bool BoolValue
{
get { return _bool; }
}

public int IntValue2
{
get { return _int2; }
}

public int IntValue1
{
get { return _int1; }
}
}

SampleClass s1 = new SampleClass(10, 20, false, "string2");
SampleClass s2 = new SampleClass(50, 100, true,
"loooooooooooooooong string");

BinaryFormatter f = new BinaryFormatter();
using (FileStream fs = new FileStream(@"d:\f2", FileMode.Create))
{
f.Serialize(fs, s1);
f.Serialize(fs, s2);
}

SampleClass res1, res2;
using (FileStream fs = new FileStream(@"d:\f2", FileMode.Open))
{
res1 = (SampleClass)f.Deserialize(fs);
res2 = (SampleClass)f.Deserialize(fs);
}

Debug.Assert(res1.BoolValue == s1.BoolValue);
Debug.Assert(res1.IntValue1 == s1.IntValue1);
Debug.Assert(res1.IntValue2 == s1.IntValue2);
Debug.Assert(res1.StringValue == s1.StringValue);

Debug.Assert(res2.BoolValue == s2.BoolValue);
Debug.Assert(res2.IntValue1 == s2.IntValue1);
Debug.Assert(res2.IntValue2 == s2.IntValue2);
Debug.Assert(res2.StringValue == s2.StringValue);

Jan 25 '06 #2
Ahmad,

Can you post your code with an example?

I would ^think^ that there would be a marker that the serialization
engine sets for the end of the serialized object, so that you can serialize
one object after another from the same stream. However, from what I
remember, there are no guarantees that is in fact what it does.

What you could do is serialize to a temp stream (MemoryStream) and then
prefix whatever you write to your final stream with the length. You can
then read the length, then read the characters out to a byte array which you
construct a MemoryStream from and then deserialize your instance from.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ahmad Jalil Qarshi" <ah*********@SPAMhotmail.com> wrote in message
news:O4**************@TK2MSFTNGP11.phx.gbl...
Hi!

I have a class named CClassToSerialize that contain some boolean, string
and few custom variable types.

Now when I create only a single Object and Serialize/Deserialize
everything is fine.

But I want to save more than one Object of this class into same file.

Now the problem is that if I create first object and Serialize it after
initializing some variables. Size of file is 905 bytes. Again when I
create another object of the same class and Serialize it after
Initializing same variables but with different values the size of the file
becomes 1,799 bytes. which is not double of 905.

Now I dont know how to Deserialize each object independently.

So anybody there to help me how to achieve this task.

Thanks in Advance

Ahmad Jalil Qarshi

Jan 25 '06 #3
Hiya;

Vladimir's solution looks more graceful, but as a cheap solution: just load
the objects into an array and serialize the (single) array instance?

Marc
Jan 25 '06 #4
Thanks Vladimir it worked fine.

But few things confused me.

How can I find the Number of Serialized Objects in a file?

Furthermore is it possible to Deserialize a specific object?

Thanks and Regards,

Ahmad Jalil Qarshi

"Vladimir Matveev" <de******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
[Serializable]
public class SampleClass
{
private int _int1;
private int _int2;
private bool _bool;
private string _string;

public SampleClass(int i1, int i2, bool b, string s)
{
_int1 = i1;
_int2 = i2;
_bool = b;
_string = s;
}

public string StringValue
{
get { return _string; }
}

public bool BoolValue
{
get { return _bool; }
}

public int IntValue2
{
get { return _int2; }
}

public int IntValue1
{
get { return _int1; }
}
}

SampleClass s1 = new SampleClass(10, 20, false, "string2");
SampleClass s2 = new SampleClass(50, 100, true,
"loooooooooooooooong string");

BinaryFormatter f = new BinaryFormatter();
using (FileStream fs = new FileStream(@"d:\f2", FileMode.Create))
{
f.Serialize(fs, s1);
f.Serialize(fs, s2);
}

SampleClass res1, res2;
using (FileStream fs = new FileStream(@"d:\f2", FileMode.Open))
{
res1 = (SampleClass)f.Deserialize(fs);
res2 = (SampleClass)f.Deserialize(fs);
}

Debug.Assert(res1.BoolValue == s1.BoolValue);
Debug.Assert(res1.IntValue1 == s1.IntValue1);
Debug.Assert(res1.IntValue2 == s1.IntValue2);
Debug.Assert(res1.StringValue == s1.StringValue);

Debug.Assert(res2.BoolValue == s2.BoolValue);
Debug.Assert(res2.IntValue1 == s2.IntValue1);
Debug.Assert(res2.IntValue2 == s2.IntValue2);
Debug.Assert(res2.StringValue == s2.StringValue);

Jan 26 '06 #5
well you can do this in this way,

BinaryFormatter bformatter;
stream = File.Open("temp.osl", FileMode.Open);
bformatter = new BinaryFormatter();
while(stream.Position!=stream.Length) // This is the trick !!
{

your_class_object = (your_class)bformatter.Deserialize(stream);

}

stream.Close();

Got it ?
cheers !

Feb 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin. D. Waller | last post by:
Hello, Does anyone kow of any good examples or documents that talk about serializing complex objects and object graphs to XML. Basically I would like to have a class that references other...
2
by: Hollywood | last post by:
After doing a search through google's archives of this list, I didn't see what I was looking for so here goes... Is it possible to serialize/deserialize multiple objects from a single XML file? ...
2
by: Greg | last post by:
Hi all, Is there a way to copy multiple objects into the clipboard and then paste them? What I want to achive is to be able to copy UI controls (textedits, dropdowns, etc) from one form and...
0
by: tayssir.hawary | last post by:
Dear all, I need to get the old value when multiple objects are selected. It works fine when a single object is selected, but e.OldValue contains null when multiple objects are selected. I...
0
by: JackRazz | last post by:
I'm trying to serialize a collection to a file stream by serializing each object individually. The code below works fine with the BinaryFormatter, but the SoapFormatter reads the first object and...
0
by: Redowl | last post by:
Apologies if this has been covered in an early question, but I am having difficulty serializing a derived class. The base class has a property which is marked as MustOverride, but when I serialize...
3
by: Thyme | last post by:
Each time I serialize an object using XmlSerializer I get a structure like this: <?xml version="1.0"?> <MyType> .. .. </MyType> <?xml version="1.0"?> <MyType> ..
1
by: Andrew Poulos | last post by:
I'm building some e-learning that's managed by an learning management system (LMS). The LMS allows only one "field" for me to store custom lesson data. The e-learning currently uses a number of...
2
by: tshad | last post by:
I tried to put multiple objects in one using statements like so: using (fs = new FileStream(fromImagePath + "\\" + (string)dr, FileMode.Open, System.IO.FileAccess.Read) , BinaryReader br = new...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.