Deserializing objects into a different class names

D

Drolem

Hello All,

I use the following method to serialize a settings class to a string:

'=======================================================================
Public Shared Function DeflateClass(ByVal serializing As Object, ByVal
typeSerializing As System.Type) As String

Dim writer As StringWriter = New StringWriter
Dim serializer As XmlSerializer = _
New XmlSerializer(typeSerializing)

serializer.Serialize(writer, serializing)

Return writer.ToString()

End Function
'=======================================================================

And I use the following method to deserialize a string into a
settings class:

'=======================================================================
Public Shared Function InflateClass(ByVal serialized As String, ByVal
typeSerialized As System.Type) As Object

Dim serializer As XmlSerializer = _
New XmlSerializer(typeSerialized)
Dim reader As StringReader = _
New StringReader(serialized)

Return serializer.Deserialize(reader)

End Function
'=======================================================================

Everything works good, BUT ... the name of my settings class has
changed. So now when I try and deserialize existing data I get:

System.InvalidOperationException: There is an error in XML document
(2, 2). ---> System.InvalidOperationException: <MySettings xmlns=''>
was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read4_ExecuterSettings()
--- End of inner exception stack trace ---

This is because the serialized XML root node name (MySettings) does
not match the settings class name (ProcessSettings). I can easily fix
the problem by editing the XML and changing the name of the root node
(from MySettings to ProcessSettings).

Now, there must be a better way, right?

I did some research and found the SerializationBinder class. However,
this seems to only work with the BinaryFormatter or the SoapFormatter
and not with the XmlSerializer. So, what I have ended up with is the
following method:

'=======================================================================
Public Shared Function InflateClass(ByVal serialized As String, ByVal
typeSerialized As System.Type) As Object

Dim existingDoc As XmlDocument = New XmlDocument
existingDoc.LoadXml(serialized)
Dim existingRoot As XmlNode = existingDoc.DocumentElement

Dim newDoc As XmlDocument = New XmlDocument
Dim newRoot As XmlNode = newDoc.CreateElement(typeSerialized.Name)
newRoot.InnerXml = existingRoot.InnerXml
newDoc.AppendChild(newRoot)

serialized = newDoc.OuterXml()

Dim serializer As XmlSerializer = _
New XmlSerializer(typeSerialized)
Dim reader As StringReader = New StringReader(serialized)
Return serializer.Deserialize(reader)

End Function
'=======================================================================

This works "okay", but I am looking for a better solution. Maybe I am
stuck with this if I continue to use the XmlSerializer instead of
SoapFormatter.

Has anyone had this problem with the XmlSerializer and found a better
solution?

Thanks for any help you can give :)
 
J

Jay B. Harlow [MVP - Outlook]

Drolem,
Now, there must be a better way, right?
Not necessarily. ;-)
This is because the serialized XML root node name (MySettings) does
not match the settings class name (ProcessSettings). I can easily fix
the problem by editing the XML and changing the name of the root node
(from MySettings to ProcessSettings).
Have you considered using an XSLT transform to enable the program to change
the MySettings node to ProcessSettings?

I have not done a lot with XML serialization, I understand that you can use
the System.Xml.Serialization.XmlRootAttribute on your class to control the
root name of the XML created. There are a number of other attributes in
System.Xml.Serialization that you may find helpful.

Hope this helps
Jay
 
D

Drolem

Ya, it seems like the System.Xml.Serialization.XmlRootAttribute will
help in writing the classes. My main problem is I have lots of these
Xml files to convert and some older processes still using the old
format.

Well, looks like I am stuck until I can upgrade my older software to
use a newer format.

Thanks for your info :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top