473,387 Members | 1,721 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,387 software developers and data experts.

Serialize derived instances to XML

Hi,

I have actually found 2 ways to do this. One uses the XmlInclude
attribute in the base class A, specifying the valid derived types to
deserialize from the stream, and one uses one of the 8 XmlSerializer
ctor's to specify base class A and derived class B relationships.

However, XmlInclude only works at compile-time, and I would like to
support serialisation for future derived classes C in other modules.
So in this case, this option is not practical.

The second option works if your data (de)serialisation is starting in
class Z instances that have the base A or derived instances B,C
directly as members. But if these class Z instances themseves are
already tagged with the [Serializable] attribute and are serialized
somewhere else (possibly in a deep hierarchy), you end up with a bit
of a nasty problem:

It forces you to implement the IXmlSerializable interface on a level
(class Z) which would otherwise serialize just fine, except for those
data members that derive from the base class A. You have to implement
customized serialisation methods at that level because you need to be
able to construct the serializer for that member (one of the derived
instances B,C of A). Things can get even more nasty when these
instances are kept in a container structure like e.g. [XmlArray] where
it is actually the tagged container that serialises it's members. So,
in this special case, you end up re-implementing custom list
(de)serialisation for a container structure that otherwise already
worked fine!

Possibly I missed another option? What would be the preferred way to
deal with this situation correctly?

Cheers!
a0a

Jun 27 '08 #1
0 1036

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

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
8
by: Gonçalo Rodrigues | last post by:
Hi all, I have a template class (call it Object) whose instances have a variable size part - an array of of T objects. But this variable size part is fixed at creation time so instead of...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
1
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
0
by: Romain TAILLANDIER | last post by:
Hi group I am quite new to xml. I have a strong typed collection derived from a collection base, and i need to serialize it. I have seriously search on the net and don't find any help about how...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
3
by: Julie | last post by:
Here's the scenario (public attributes, etc. omitted for brevity): class Base { } class Derived : Base { }
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.