xml serialize private properties with a wrapper class -- how to?

  • Thread starter Thread starter Samuel R. Neff
  • Start date Start date
S

Samuel R. Neff

We're using XmlSerializer to serialize our data objects for
persistence in a WinForms app. This is our primary data store, we're
not web connected and are not using a database. We've had to program
some of our classes in undesired ways to support XmlSerializer and
it's limitations (public new constructor, only public properties, no
dictionary support, etc).

We just came across a note in one of the articles on XML Serialization
that says you can work around some of these issues with a wrapper
class, but there's no link for more info. What is this talking about
and how can we better support XmlSerializer based persistence without
having to mess with our object model?

We've also looked at using the SoapTypeImporter which provided little
additional benefit and the SoapFormatter which created undesirable XML
(one of the main reasons for using XmlSerializer is to allow
interoperability with other non .net apps and simple XML is a big
plus).

Thanks,

Sam


XML Serialization in the .NET Framework
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml01202003.asp

Q: How can I serialize classes that were not designed for XML
serialization if I do not want to use the SoapFormatter?

A: You can design special wrapper classes that expose or hide fields
and properties from the XmlSerializer.
 
Let's say you have

Public Class Customer
Protected Address as String
End Class

Address wouldn't serialize. But you could do:

Public Class CustomerSerial
Inherits Customer

Public Property AddressString
Get
Return Address
End Get
Set
Address = Value
End Set
End Property
End Class.

Now, you could do:

Dim c as New Customer
Dim s as CustomerSerial = c

s would serialize and deserialize just fine.

Hope this helps

- Scott Swigart
http://blog.swigartconsulting.com
 
Back
Top