Unable to DeSerialize using DataContractSerializer

34 views Asked by At

I have been stuck for days and need help

I am able to serialize the NewMyClass2 but unable to De-Serialize it. Below is the code SetFileInfo to Serialize and GetFileInfo to De-Serialize

        static public void SetFileInfo<T>(string strMMPName, T SerializableData)
        {
            try
            {
                MemoryMappedFile mmf;
                try
                {
                    mmf = MemoryMappedFile.CreateNew(strMMPName, 1000000, MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileOptions.DelayAllocatePages, HandleInheritability.Inheritable);
                }
                catch (Exception ex)
                {
                    //get error when MMP File is already present then proceed to open.
                }

                mmf = MemoryMappedFile.CreateOrOpen(strMMPName, 1000000);

                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));

                    using (var writer = XmlDictionaryWriter.CreateBinaryWriter(stream))
                    {
                        dataContractSerializer.WriteObject(writer, SerializableData);
                        writer.Flush();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("SetFileInfo - " + ex.ToString());
            }
        }

        static public T GetFileInfo<T>(string strMMPName)
        {
            T SerializableData = default(T);

            try
            {
                MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(strMMPName, 1000000);

                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));

                    stream.Position = 0;
                    using (var reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max)) //here reader is "None"
                    {
                        try
                        {
                            SerializableData = (T)dataContractSerializer.ReadObject(reader);
                        }
                        catch (Exception ec)
                        {
                           //"There was an error deserializing the object of type SerializeDeserialize.NewMyClass2. The input source is not correctly formatted."
                            MessageBox.Show("deSerialize - " + ec.ToString());
                        }
                        return SerializableData;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetFileInfo - " + ex.ToString());
            }

            return SerializableData;
        }

Below is DataContractSerializable Class

    [DataContract]
    public class NewMyClass2 
    {
        [DataMember]
        public string Id { get; set; }

        [DataMember]
        public DateTime Setdatetime { get; set; }


        
        public NewMyClass2()
        {
            Id = "";
            Setdatetime = default(DateTime);      
        }
    }

and this how i am trying to call Serialize or De-Serialize

            NewMyClass2 myClass2 = new NewMyClass2();
            NewMyClass2 myClass22 ;
            myClass2.Id = "12345678";
            myClass2.Setdatetime = DateTime.Now;

            DeOrSerialize.SetFileInfo("UpdateTime", myClass2);

            myClass22 = DeOrSerialize.GetFileInfo<NewMyClass2>("UpdateTime");

error which i am getting in GetFileInfo is "There was an error deserializing the object of type SerializeDeserialize.NewMyClass2. The input source is not correctly formatted."

I should be getting the data when i De-Serialize

0

There are 0 answers