I am using the following methods to serialize objects into xml:
VB Code:
Dim oSerializer As New Runtime.Serialization.DataContractSerializer(oSourceObject.GetType) 'if we have a serializer object If (oSerializer IsNot Nothing) Then Using oStream As New IO.FileStream(sFileName, IO.FileMode.Create) 'write the object oSerializer.WriteObject(oStream, oSourceObject) 'close the stream oStream.Close() 'set the result to true bResult = True End Using End If 'if we have a serializer object
This works fine, but if I was to later extend the object by say adding properties etc, then when I attempt to deserialize the original serialized file, it fails due to the fact that the objects are not identical (here is the deserialization code):
VB Code:
Dim oSerializer As New Runtime.Serialization.DataContractSerializer(oTargetObject.GetType) 'if we have a serializer object If (oSerializer IsNot Nothing) Then Dim oStream As New IO.FileStream(sFileName, IO.FileMode.Open) Dim oReader As Xml.XmlDictionaryReader = Xml.XmlDictionaryReader.CreateTextReader(oStream, New Xml.XmlDictionaryReaderQuotas()) 'attempt to read the object Try 'deserialize the data and read it from the instance oTargetObject = oSerializer.ReadObject(oReader, True) 'set the result to true bResult = True Catch ex As Exception 'throw the error to the calling application Throw New Exception(String.Empty, ex) Finally 'close the reader If (oReader IsNot Nothing) Then oReader.Close() 'close the file stream If (oStream IsNot Nothing) Then oStream.Close() End Try End If 'if we have a serializer object
Is there a way to populate the object without failing? In other words, just skip over the missing properties.




Reply With Quote