473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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: EmailDestination,
FileDestination and FtpDestination. Ultimately, I need to be able to
serialize the Filter instance and all of its properties.

I've implement the IXmlSerializable interface in the EmailDestination,
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.Create)
Dim xs As New System.Xml.Serialization.XmlSerializer(GetType(Fil ter))
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 IXmlSerializable.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
Sep 22 '06 #1
1 3340
Solved my own problem. I needed to inherit from a base class, then decorate
the base class with the XmlInclude attribute.

<XmlInclude(GetType(EmailDestination)), XmlInclude(GetType(FtpDestination)),
XmlInclude(GetType(FileDestination)) _
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" <so*****@somewhere.comwrote in message
news:eM**************@TK2MSFTNGP06.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: EmailDestination,
FileDestination and FtpDestination. Ultimately, I need to be able to
serialize the Filter instance and all of its properties.

I've implement the IXmlSerializable interface in the EmailDestination,
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.Create)
Dim xs As New System.Xml.Serialization.XmlSerializer(GetType(Fil ter))
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 IXmlSerializable.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

Sep 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Just D. | last post by:
There is an interesting article - http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=542&printer=t It shows how to serialize the ArrayList of identical objects. I did it a year...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
3
by: RandomEngineer | last post by:
So here's the challenge... How can a collection (System.Collections.Generic.IList) of some custom type be serialized in a web service using .NET 2.0? Below are the class and the web methods in...
3
by: Dave | last post by:
Please - anyone that can help. I am getting confusing results while trying to expose a collection from a web service. I have a webservice in which a web method accepts a collection as a...
6
by: SharpCoderMP | last post by:
hi, until .net 2.0 i've been writing my own collections derived from CollectionBase. Now with the generics it is much easier to do that... but! i have some questions about generic lists of...
0
by: Mathieu Cartoixa | last post by:
Hi, I have a simple 2-tiers (client+database) application with simple Domain Model objects The Data Access Layer is abstracted via Data Mappers which use Data Transfer Objects to communicate...
1
by: DesperateDan | last post by:
I've got a parent object that is a collection and it in turn is hosting 2 child objects. My searlization has worked perfectly to plan in as much that I wanted to dictate the names of the objects...
4
by: xke | last post by:
Why the collection property is not included in the ouput serialization ? I have a custom generic collection (implements icollection): Events of objects: Event. Event is a simple class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.