471,319 Members | 1,211 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 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 13068
[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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Martin. D. Waller | last post: by
2 posts views Thread by Greg | last post: by
reply views Thread by Redowl | last post: by
1 post views Thread by Andrew Poulos | last post: by
2 posts views Thread by tshad | last post: by
reply views Thread by rosydwin | last post: by

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.