Collection of generic objects that can be xml serialized?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Buchanan

    #1

    Collection of generic objects that can be xml serialized?

    I have a class named 'Filter'. It has a property named 'Destinations' (an
    ArrayList). I would like the Destinations property to be a collection of
    objects. The objects that can be collected are: EmailDestinatio n,
    FileDestination and FtpDestination. Ultimately, I need to be able to
    serialize the Filter instance and all of its properties.

    I've implement the IXmlSerializabl e interface in the EmailDestinatio n,
    FileDestination and FtpDestination classes.

    Unfortunately, when I try to serialize an instance of the Filter class using
    this code:

    fs = New FileStream(_Url , IO.FileMode.Cre ate)
    Dim xs As New System.Xml.Seri alization.XmlSe rializer(GetTyp e(Filter))
    xs.Serialize(fs , _Filter)

    I get an error on the last line that reads:

    The type FileDestination was not expected. Use the XmlInclude or
    SoapInclude attribute to specify types that are not known statically.

    I figured that the IXmlSerializabl e.WriteXml method would be called during
    the xs.Serialize() process, but it isn't.

    I've experimented with different types of collections (basing the
    Destinations on the CollectionBase class), inheriting the FileDestination
    from an abstract class and having the FileDestination implement an
    interface. All result in a serialization error.

    Is there a way to xml serialize a collection of generic classes? If so, I'd
    be grateful for some guidance.

    Thanks,

    Craig Buchanan


  • Craig Buchanan

    #2
    Re: Collection of generic objects that can be xml serialized?

    Solved my own problem. I needed to inherit from a base class, then decorate
    the base class with the XmlInclude attribute.

    <XmlInclude(Get Type(EmailDesti nation)), XmlInclude(GetT ype(FtpDestinat ion)),
    XmlInclude(GetT ype(FileDestina tion)) _
    Public MustInherit Class Destination
    Public Name As String

    Public Sub New()
    End Sub

    End Class

    Does anyone know of a way to move this annotation to the derived class?
    Seems like the Base class should be unaware of its derivations.

    Thanks,

    Craig


    "Craig Buchanan" <someone@somewh ere.comwrote in message
    news:eMt9lfl3GH A.1796@TK2MSFTN GP06.phx.gbl...
    >I have a class named 'Filter'. It has a property named 'Destinations' (an
    >ArrayList). I would like the Destinations property to be a collection of
    >objects. The objects that can be collected are: EmailDestinatio n,
    >FileDestinatio n and FtpDestination. Ultimately, I need to be able to
    >serialize the Filter instance and all of its properties.
    >
    I've implement the IXmlSerializabl e interface in the EmailDestinatio n,
    FileDestination and FtpDestination classes.
    >
    Unfortunately, when I try to serialize an instance of the Filter class
    using this code:
    >
    fs = New FileStream(_Url , IO.FileMode.Cre ate)
    Dim xs As New System.Xml.Seri alization.XmlSe rializer(GetTyp e(Filter))
    xs.Serialize(fs , _Filter)
    >
    I get an error on the last line that reads:
    >
    The type FileDestination was not expected. Use the XmlInclude or
    SoapInclude attribute to specify types that are not known statically.
    >
    I figured that the IXmlSerializabl e.WriteXml method would be called during
    the xs.Serialize() process, but it isn't.
    >
    I've experimented with different types of collections (basing the
    Destinations on the CollectionBase class), inheriting the FileDestination
    from an abstract class and having the FileDestination implement an
    interface. All result in a serialization error.
    >
    Is there a way to xml serialize a collection of generic classes? If so,
    I'd be grateful for some guidance.
    >
    Thanks,
    >
    Craig Buchanan
    >

    Comment

    Working...