CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2006
    Posts
    38

    Serialization problem

    Hi Every body
    I have a problem and want help.
    I have created a phonebook application and it works fine after a awhile i liked to make an upgrade for my application and i started from scratch i didn't inherit it from my old class,and i successes too ,my request
    "I want to migrate my contacts from the old application to the
    new one"

    ,so i made an adapter class for this reason in my new application with the following code
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace PhoneBook
    {
        class Adapter
        {
            PhoneRecord PhRecord;   //the new application object
            CTeleRecord TelRecord; //the old application object
            string fileName;
            
            public Adapter(string filename)
            {
                fileName = filename;
            }
    
            public void convert()
            {
    
                PhRecord = new PhoneRecord(); 
                TelRecord = new CTeleRecord();
    
                FileStream OpFileSt = new FileStream(fileName, FileMode.Open,FileAccess.Read);
              
    
                BinaryFormatter readBin = new BinaryFormatter();
             
    
    
                for (; ; )
                {
                    try
                    {
                        TelRecord.ResetTheObject();
    
                        TelRecord = (CTeleRecord)readBin.Deserialize(OpFileSt);
    
                        PhRecord.SetName = TelRecord.GetName;
                        PhRecord.SetHomeNumber = TelRecord.GetHomeNumber;
                        PhRecord.SetMobileNumber = TelRecord.GetMobileNumber;
                        PhRecord.SetWorkNumber = TelRecord.GetWorkNumber;
                        PhRecord.SetSpecialNumber = TelRecord.GetSpecialNumber;
                        PhRecord.SetEmail = TelRecord.GetEmail;
                        PhRecord.SetNotes = TelRecord.GetNotes;
                        PhBookContainer.phBookItems.Add(PhRecord);
    
                        
                    }
                    catch (IOException xxx)
                    {
                        MessageBox.Show(xxx.Message);
    
    
                    }
                    catch (ArgumentException tt)
                    {
                        MessageBox.Show(tt.Message);
                    }
                    //if end of file is reached
                    catch (SerializationException x)
                    {
                        MessageBox.Show(x.Message + x.Source);
                        break;
                    }
    the problem is when i try to read the file ctreated by my old application i receive serialization exception with this message
    "Unabel to find assembly 'PhoneBook,Version=1.0.0.0,Culture=neutral,PublicK eyToken=null"

    and the source of exceptionis mscorlib.


    when i read the same file with my old application(Which is the origin of the file)i have no problem and idon't know what to do to make my

    adapter class work.so can somebody help please.

  2. #2
    Join Date
    Mar 2007
    Posts
    59

    Re: Serialization problem

    Just as a tip, it's usually not a good idea to use exception for handling normal behaviors in the code (like finding the end of file). An exception is supposed to be exceptional. Would be better this way:

    Code:
    try { while(!end_of_file) { ... } } catch () {}
    Anyway, to return to your problem, it's difficult to tell exactly what is the problem, but it seems that there is a mismatch in the binary format. When reading the file, I think it uses the mangling from your old app and trying to find the type. So, maybe try to decorate your CTeleRecord with the same namespace from your old application.
    Last edited by angedelamort; March 22nd, 2010 at 07:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured