Serialize Object by Object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seshamsri
    New Member
    • Feb 2008
    • 4

    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.
  • jjvainav
    New Member
    • Feb 2008
    • 25

    #2
    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

    Comment

    • seshamsri
      New Member
      • Feb 2008
      • 4

      #3
      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.

      Comment

      • seshamsri
        New Member
        • Feb 2008
        • 4

        #4
        But lets assume a scenario,
        Each time an application runs ,say we want to serialize only a big single object(assume,e verytime 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.

        Comment

        • jjvainav
          New Member
          • Feb 2008
          • 25

          #5
          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 .

          Comment

          • seshamsri
            New Member
            • Feb 2008
            • 4

            #6
            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.

            Originally posted by jjvainav
            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 .

            Comment

            Working...