changing class that is serialized

qex

Well-known member
Joined
Apr 2, 2003
Messages
78
I have a class that Im using for configuration of part of my app. The serializtion and deserialization work fine. The problem Im having is if I want to add a new property to the class that is serialized then the old xml config file will not load because now the class has more members than the serialized xml file.

Ive found some information on the binder memeber of IFormatter class, but this seems like overkill. I would just like it to load the values that it can and then rewrite the file when I save it after the new member is added. Easier said than done?
 
Ok I figured something out. I had been using the SoapFormatter for my serialization. I found this code on the net it works as I hoped it doesnt mind if I have changed my class and added properties when it loads the data.

/// <summary>
/// Serializes an object instance to a file.
/// </summary>
/// <param name="Instance">the object instance to serialize</param>
/// <param name="Filename"></param>
/// <param name="BinarySerialization">determines whether XML serialization or binary serialization is used</param>
/// <returns></returns>

public static bool SerializeObject(object Instance, string Filename,
bool BinarySerialization)
{
bool retVal = true;
if (!BinarySerialization)
{
XmlTextWriter writer = null;
try
{
XmlSerializer serializer =
new XmlSerializer(Instance.GetType());
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(Filename, FileMode.Create);
writer = new XmlTextWriter(fs, new UTF8Encoding());
writer.Formatting = Formatting.Indented;
writer.IndentChar = ;
writer.Indentation = 3;
// Serialize using the XmlTextWriter.
serializer.Serialize(writer,Instance);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
retVal = false;
}
finally
{
if (writer != null)
writer.Close();
}
}
else
{
Stream fs = null;
try
{
BinaryFormatter serializer = new BinaryFormatter();
fs = new FileStream(Filename, FileMode.Create);
serializer.Serialize(fs,Instance);
}
catch
{
retVal = false;
}
finally
{
if (fs != null)
fs.Close();
}
}
return retVal;
}



/// <summary>
/// Deserializes an object from file and returns a reference.
/// </summary>
/// <param name="Filename">name of the file to serialize to</param>
/// <param name="ObjectType">The Type of the object. Use typeof(yourobject class)</param>
/// <param name="BinarySerialization">determines whether we use Xml or Binary serialization</param>
/// <returns>Instance of the deserialized object or null. Must be cast to your object type</returns>

public static object DeSerializeObject(string Filename,Type ObjectType,
bool BinarySerialization)
{
object Instance = null;

if (!BinarySerialization)
{
XmlReader reader = null;
XmlSerializer serializer = null;
FileStream fs = null;
try
{
// Create an instance of the XmlSerializer specifying type and namespace.
serializer = new XmlSerializer(ObjectType);
// A FileStream is needed to read the XML document.
fs = new FileStream(Filename, FileMode.Open);
reader = new XmlTextReader(fs);
Instance = serializer.Deserialize(reader);
}
catch
{
return null;
}
finally
{
if (fs != null)
fs.Close();
if (reader != null)
reader.Close();
}
}
else
{
BinaryFormatter serializer = null;
FileStream fs = null;
try
{
serializer = new BinaryFormatter();
fs = new FileStream(Filename, FileMode.Open);
Instance = serializer.Deserialize(fs);
}
catch
{
return null;
}
finally
{
if (fs != null)
fs.Close();
}
}
return Instance;
}
 
Back
Top