Hi!
I have a class named CClassToSeriali ze 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 5 13467
[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,
"looooooooooooo ooong 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(re s1.BoolValue == s1.BoolValue);
Debug.Assert(re s1.IntValue1 == s1.IntValue1);
Debug.Assert(re s1.IntValue2 == s1.IntValue2);
Debug.Assert(re s1.StringValue == s1.StringValue) ;
Debug.Assert(re s2.BoolValue == s2.BoolValue);
Debug.Assert(re s2.IntValue1 == s2.IntValue1);
Debug.Assert(re s2.IntValue2 == s2.IntValue2);
Debug.Assert(re s2.StringValue == s2.StringValue) ;
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.co m
"Ahmad Jalil Qarshi" <ah*********@SP AMhotmail.com> wrote in message
news:O4******** ******@TK2MSFTN GP11.phx.gbl... Hi!
I have a class named CClassToSeriali ze 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
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
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.goo glegroups.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, "looooooooooooo ooong 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(re s1.BoolValue == s1.BoolValue); Debug.Assert(re s1.IntValue1 == s1.IntValue1); Debug.Assert(re s1.IntValue2 == s1.IntValue2); Debug.Assert(re s1.StringValue == s1.StringValue) ;
Debug.Assert(re s2.BoolValue == s2.BoolValue); Debug.Assert(re s2.IntValue1 == s2.IntValue1); Debug.Assert(re s2.IntValue2 == s2.IntValue2); Debug.Assert(re s2.StringValue == s2.StringValue) ;
well you can do this in this way,
BinaryFormatter bformatter;
stream = File.Open("temp .osl", FileMode.Open);
bformatter = new BinaryFormatter ();
while(stream.Po sition!=stream. Length) // This is the trick !!
{
your_class_obje ct = (your_class)bfo rmatter.Deseria lize(stream);
}
stream.Close();
Got it ?
cheers ! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 classes etc and I'd like to serialize
the whole thing...
Many thanks...
Martin
|
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? I was looking to use the XML serialization to be able to translate
complex XML elements in a single XML file through XML serialization as
opposed to using SAX and coding my own serializer. However, the XML
Serialization API appears to only...
|
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 paste them into another form.
Your help is much appriciated. Thanks in advance,
Greg.
|
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 appreciate a reply:)
Tayssir Hawary
|
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 just goes to the end of the file. After
reading the first object, the fStream.Position is pointed to the end of the file.
The collection serializes/deserilizes fine when I just serialize the hashtable vs.
each object in the hashtable.
Does...
| |
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 the derived class, the value
for this property is lost.
Grateful for any help.
Alex
|
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>
..
|
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 separate
javascript objects.
How can I concatenate them into one object (to JSON string them to then
feed them to the LMS) and then to later parse the JSON string back into
the original multiple objects?
Andrew Poulos
|
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
BinaryReader(fs))
But I get an error:
") expected"
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |