Serializing Multiple Objects of a Class

A

Ahmad Jalil Qarshi

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
 
V

Vladimir Matveev

[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);
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Marc Gravell

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
 
A

Ahmad Jalil Qarshi

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
 
A

asanga.lk

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 !
 

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