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
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