Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented Serialization in my small project. It works fine but deserialization does not work. Can any one send me some basic article about it?

I have implemented SERIALIZATION in the following way:

MIDL
// SERIALIZATION
output = new FileStream( "AppManager.am", FileMode.OpenOrCreate, FileAccess.Write );
for (int i = 0; i < m_Appointments.Count; i++)
    formatter.Serialize(output, m_Appointments[i]);
output.Close();


I know the number of objects to be written but in case of DESERIALIZATION, how to know it?
Posted

There is an article on the subject here: Object Serialization using C#[^]
 
Share this answer
 
Just do
formatter.Serialize(output, m_Appointments)
The main collection classes are serialisable.

Then you can simply do
m_Appointments = formatter.Deserialize(input);

Generally for this type of thing you want to have one root object which is serialisable, and simply dump that object to file with the serialiser. So reading it back is just a case of reading one object.
 
Share this answer
 
Comments
Mushtaq Muhammad 4-Apr-11 10:19am    
In my main program, I have my main data as a LIST of Appointment type objects like

public List<appointment> m_Appointments = new List<appointment>();

I have made the Class Appointment as Serializable.

Now I want to De-serialize this List and it is not working. My code is looks like

public frmAppManager()
{
InitializeComponent();

// DE-SERIALIZATION
output = new FileStream("AppManager.am", FileMode.Open, FileAccess.Read);
if(output.Length != 0)
m_Appointments = (List<appointment>) formatter.Deserialize(output);

output.Close();
}


Similarly, I am using the following code in somewhere else place for SERIALIZATION

// SERIALIZATION
output = new FileStream( "AppManager.am", FileMode.OpenOrCreate, FileAccess.Write );
for (int i = 0; i < m_Appointments.Count; i++)
formatter.Serialize(output, m_Appointments[i]);
output.Close();


Unfortunatly, De-serialization is not working. Actually, it cannot cast properly. Please suggest more clearly.
BobJanova 4-Apr-11 13:14pm    
The cast in deserialisation is fine, but you didn't change serialisation as my comment suggested.
Mushtaq Muhammad 6-Apr-11 11:19am    
Thanks for your kind reply. I have resolved the issue by using the following way.
I have first serialize the count of the list and then serialize the whole list by using for loop.
For desrialization, I have first read back the count and then all elements by using a for loop.
Thanks again for your time.
Here is some code that will do serialization and deserialization with generic methods:

XML
/// <summary>
/// Serializes an object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializableObject"></param>
/// <param name="fileName"></param>
public void SerializeObject<T>(T serializableObject, string fileName)
{
    if (serializableObject == null) { return; }
    try
    {
        XmlDocument xmlDocument = new XmlDocument();
        XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
        using (MemoryStream stream = new MemoryStream())
        {
            serializer.Serialize(stream, serializableObject);
            stream.Position = 0;
            xmlDocument.Load(stream);
            xmlDocument.Save(fileName);
            stream.Close();
        }
    }
    catch (Exception ex)
    {
        //Log exception here
    }
}


/// <summary>
/// Deserializes an xml file into an object list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fileName"></param>
/// <returns></returns>
public T DeSerializeObject<T>(string fileName)
{
    if (string.IsNullOrEmpty(fileName)) { return default(T); }
    T objectOut = default(T);
    try
    {
        string attributeXml = string.Empty;
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(fileName);
        string xmlString = xmlDocument.OuterXml;

        using (StringReader read = new StringReader(xmlString))
        {
            Type outType = typeof(T);
            XmlSerializer serializer = new XmlSerializer(outType);
            using (XmlReader reader = new XmlTextReader(read))
            {
                objectOut = (T)serializer.Deserialize(reader);
                reader.Close();
            }
            read.Close();
        }
    }
    catch (Exception ex)
    {
        //Log exception here
    }
    return objectOut;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900