Results 1 to 9 of 9

Thread: [RESOLVED] XML Serialization/Deserialization

  1. #1

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Resolved [RESOLVED] XML Serialization/Deserialization

    I am using the following methods to serialize objects into xml:

    VB Code:
    1. Dim oSerializer As New Runtime.Serialization.DataContractSerializer(oSourceObject.GetType)
    2.  
    3.                 'if we have a serializer object
    4.                 If (oSerializer IsNot Nothing) Then
    5.  
    6.                     Using oStream As New IO.FileStream(sFileName, IO.FileMode.Create)
    7.  
    8.                         'write the object
    9.                         oSerializer.WriteObject(oStream, oSourceObject)
    10.  
    11.                         'close the stream
    12.                         oStream.Close()
    13.  
    14.                         'set the result to true
    15.                         bResult = True
    16.  
    17.                     End Using
    18.  
    19.                 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:
    1. Dim oSerializer As New Runtime.Serialization.DataContractSerializer(oTargetObject.GetType)
    2.  
    3.                 'if we have a serializer object
    4.                 If (oSerializer IsNot Nothing) Then
    5.  
    6.                     Dim oStream As New IO.FileStream(sFileName, IO.FileMode.Open)
    7.                     Dim oReader As Xml.XmlDictionaryReader = Xml.XmlDictionaryReader.CreateTextReader(oStream, New Xml.XmlDictionaryReaderQuotas())
    8.  
    9.                     'attempt to read the object
    10.                     Try
    11.  
    12.                         'deserialize the data and read it from the instance
    13.                         oTargetObject = oSerializer.ReadObject(oReader, True)
    14.  
    15.                         'set the result to true
    16.                         bResult = True
    17.  
    18.                     Catch ex As Exception
    19.  
    20.                         'throw the error to the calling application
    21.                         Throw New Exception(String.Empty, ex)
    22.  
    23.                     Finally
    24.  
    25.                         'close the reader
    26.                         If (oReader IsNot Nothing) Then oReader.Close()
    27.  
    28.                         'close the file stream
    29.                         If (oStream IsNot Nothing) Then oStream.Close()
    30.  
    31.                     End Try
    32.  
    33.                 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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

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

  4. #4

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: XML Serialization/Deserialization

    I think I found part of the issue, I was missing the DataMemberAttribute on the class members.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    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?

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: XML Serialization/Deserialization

    Yeah, thanks for the help!

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width