views:

39

answers:

0

My application is written in C# and I am a newbie to C#. I am facing with a problem during serialization. The serialization that is used is custom serialization using ISerializable interface. The problem is as follows.

There are many classes in my project and when serialization is called on one of the main class object it serializes itself and during this process many of the member objects are also get serialized. This whole serialization is done in to a file with extension say ".bin". This bin file size should be of size max 20MB.

In this process of serialization one of my other class also gets serialized to a stream, this class is having a big array. The size of this array or list can be of 80 MB or more. So during serialization i have serialize all the objects other than this array and depending upon the size left in the 20MB I need to add in between the serialization the number of array elements which can fit in the remaining memory.

For the other array elements left out I have to create a similar kind of .bin file with all the objects serialized and in between need to insert the number of array elements that can fit there.

The main problem here is the array elements get inserted at runtime and this list keeps on increasing. So i should be creating .bin files continuously until my application runs.

Any ideas how can i implement a solution for this problem?

Thanks in Advance.

Let me put my above question in other way. I shall serialize my main object into a file and later when required i shall open the file and parse it to the point i require and insert some extra data at that point. Is it possible in c# on a serialized file using BinaryFormatter?

I think people can better understand my problem i can explain the problem through code. Here is the code snippet of my requirement.

//*****************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SerializeNdeSerialize
{
    class Program
    {
        [Serializable]
        public struct samplepair
        {
            public double samplex;
            public double sampley;

            public samplepair(double x, double y)
            {
                samplex = x;
                sampley = y;
            }
        }

        [Serializable]
        class AHolder: ISerializable
        {
            public  ClassMember classMember = new ClassMember();
            public ClassMember2 classMember2 = new ClassMember2();

            string SerializedDatum_ClassMember = "strClassMember";
            string SerializedDatum_ClassMember2 = "strClassMember2";

            #region Serialization Constructor

            public AHolder()
            {

            }
            protected AHolder(SerializationInfo info, StreamingContext context)
            {
                if (info == null)
                {
                    throw new ArgumentNullException("info");
                }

                classMember = (ClassMember)info.GetValue(SerializedDatum_ClassMember,typeof(ClassMember));
                classMember2 = (ClassMember2)info.GetValue(SerializedDatum_ClassMember2, typeof(ClassMember2));
            }
            #endregion

            #region ISerializable Members

            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue(SerializedDatum_ClassMember, classMember);
                info.AddValue(SerializedDatum_ClassMember2, classMember2);
            }

            #endregion


        }
        [Serializable]
        class ClassMember : ISerializable
        {
            public List<samplepair> samplexypair = new List<samplepair>();
            string SerializedDatum_strXYPair = "strXYPair";

            public ClassMember()
            {

            }
            protected ClassMember(SerializationInfo info, StreamingContext context)
            {
                if (info == null)
                {
                    throw new ArgumentNullException("info");
                }

                samplexypair = (List<samplepair>)info.GetValue(SerializedDatum_strXYPair, typeof(List<samplepair>));                            
            }

            #region ISerializable Members


            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue(SerializedDatum_strXYPair, samplexypair);
            }

            #endregion
        }

        [Serializable]
        class ClassMember2 : ISerializable
        {
            public List<samplepair> samplexypair = new List<samplepair>();
            string SerializedDatum_strXYPair = "strXYPair";

            public ClassMember2()
            {

            }
            protected ClassMember2(SerializationInfo info, StreamingContext context)
            {
                if (info == null)
                {
                    throw new ArgumentNullException("info");
                }

                samplexypair = (List<samplepair>)info.GetValue(SerializedDatum_strXYPair, typeof(List<samplepair>));
            }

            #region ISerializable Members

            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue(SerializedDatum_strXYPair, samplexypair);
            }

            #endregion
        }


        static void OutputAHolder(AHolder aHolder)
        {
            foreach (samplepair pair in aHolder.classMember.samplexypair)
            {
                System.Console.WriteLine("x: {0}, y: {0}", pair.samplex, pair.sampley);
            }           
        }

        static void Main(string[] args)
        {
            AHolder objHolder = new AHolder();


            for (int i = 0; i < 20; i++)
            {
                objHolder.classMember.samplexypair.Add(new samplepair(i, i));
                objHolder.classMember2.samplexypair.Add(new samplepair(i, 23));
            }

            OutputAHolder(objHolder);

            string myFile = @"c:\MyFile.dps";
            using (MemoryStream memStream = new MemoryStream())
            {
                using (FileStream fileStream =
                    new FileStream(myFile, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    IFormatter formatter = new BinaryFormatter();

                    formatter.Serialize(fileStream, objHolder);
                }
            }

            using (FileStream fileStream = new FileStream(myFile, FileMode.Open,FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                IFormatter formatter = new BinaryFormatter();
                AHolder obHolder = (AHolder)formatter.Deserialize(fileStream);
                //****************************************************************
                //Need to add few more xy pairs here ......
                //****************************************************
                for (int i = 20; i < 40; i++)
                {
                    obHolder.classMember.samplexypair.Add(new samplepair(i, i));                    
                }

                //Problem: Need to insert these newly added xy pairs in to the stream.             //In this example stream contains only one object in my real code in the stream there can be other objects before this and after. Any idea how can solve this?????????

            }           


            using (FileStream stream =
                new FileStream(myFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                AHolder objAfterAdding;                         
                IFormatter formatter = new BinaryFormatter();

                try
                {
                    objAfterAdding = (AHolder)formatter.Deserialize(stream);
                    OutputAHolder(objAfterAdding);

                    /*AHolder objAfg = (AHolder)formatter.Deserialize(stream);
                    OutputAHolder(objAfg);*/
                }
                catch (Exception e)
                {
                     throw e;
                }               
            }
        }
    }
}

PS: Please see the comments in the code to understand my problem...