[RESOLVED] XML Serialization/Deserialization
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.
Re: XML Serialization/Deserialization
That surprises me... being XML Serialization, it should handle that better than binary serialization. And I've done that before w/o issue... in fact the files I was deserializing form weren't even created by the class I was deserializing into... they were third party generated... So there were holes aplenty.
I'd question the class definition... maybe there's something in there that is causing the hiccup. The only way it should cause is if a new field is introduced that is a "required" field of some kind, or there's a dependency.
What kind of error are you getting?
-tg
Re: XML Serialization/Deserialization
I thought it would handle it a bit better as well. So what I did for my test is serialized my object and then extended my class by adding a readonly property. I then tried to deserialize the object and get the following error:
Error in line 1 position 132. 'Element' 'iNumber' from namespace 'http://schemas.datacontract.org/2004/07/TestApp' is not expected. Expecting element 'bNewProperty'.
Re: XML Serialization/Deserialization
I think I found part of the issue, I was missing the DataMemberAttribute on the class members.
Re: XML Serialization/Deserialization
Ah... since it's read only, it can't write to it. You'll need to mark it up with the XmlIgnore attribute. That will prevent it from being serlialized/deserialized. I've often found ReadOnly props to be a pain... because they can be read, it will serialize it... but then deserialization fails because it can't be written to.
-tg
Re: XML Serialization/Deserialization
It's kind of weird because I want the property to be ReadOnly since its value is automatically generated, say like a unique ID on creation, BUT when deserializing, I want to be able to write that value back into the property...know what I mean?
Re: XML Serialization/Deserialization
I know exactly what you mean... unfortunately to read it back in... it needs to be a writable property. It bites, I know... I think to get around it, we developed two classes... virtually identical... one with all the properties read/write and contained zero logic... and one that was more.... locked down and protected and contained all of the logic... The open one was declared as a friend, or it may have been private, so that it is hidden from outside developers. Then we overlaoded the constructor of the locked down took the open one as a parameter... actually it was probalby a factory constructor now that I think about it (meaning it was static and returned an object typed as the open one) .. any ways, we would pass in the open data container and it would then copy the values over to the locked down one and return it.
Did that make sense?
-tg
Re: XML Serialization/Deserialization
Yeah, thanks for the help!
Re: [RESOLVED] XML Serialization/Deserialization
oh, and in case it wasn't obvious... only the open class is serializable... the closed one isn't...
-tg