473,750 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)serialisati on 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 IXmlSerializabl e 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)serialisati on 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 1074

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

Similar topics

14
3333
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 instance!
8
1556
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 allocating two blocks (one for the object and one for the variable-sized part) one can allocate a single block via a placement new operator like template<typename T> void* Object<T>::operator new(std::size_t sz, std::size_t number) {
5
24721
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
1
7090
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 and properties derived from this class but will not serialize or deserialize the properties in CollectionBase. Like InnerList, which is a read only property of CollectionBase. How can I serialize and deserialize the InnerList property of...
0
1358
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 to make a collection serialisable with a real name (with not the ArrayOf prefix) There is a workaround by encapsulate the collection as a property in another class and serialise that class.
26
5373
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
3
1587
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
3
10263
by: Julie | last post by:
Here's the scenario (public attributes, etc. omitted for brevity): class Base { } class Derived : Base { }
2
4183
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6803
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.