PC Review


Reply
Thread Tools Rate Thread

Deserializing objects into a different class names

 
 
Drolem
Guest
Posts: n/a
 
      9th Dec 2003
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
 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      10th Dec 2003
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


"Drolem" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Drolem
Guest
Posts: n/a
 
      11th Dec 2003
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


"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deserializing a class kbodily@charter.net Microsoft VB .NET 1 20th Jan 2007 12:47 AM
Deserializing class Jesus Microsoft VC .NET 0 23rd Mar 2004 12:48 AM
Passing arguments when deserializing objects Neal Andrews Microsoft VB .NET 2 21st Jan 2004 12:50 AM
XML: Deserializing Objects Justin Armstrong Microsoft Dot NET 1 8th Jul 2003 04:52 PM
Re: Deserializing objects Tomas Restrepo \(MVP\) Microsoft Dot NET Framework 1 8th Jul 2003 04:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.