473,498 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inherited classes and xml.serialization

suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}

public class C:A
{
public string nameA;

}

and I want to deserialise something like

<?xml?>
<A>
<Name>fred</Name>
<Contents>
<B>
<NameB>bert</NameB>
<Contents>
<A>
<Name>allison</Name>
</Contents>
</A>
<B>
<NameB>jim</NameB>
<Contents>..</Contents>
</Contents>
<A>

In short I want to represent a tree with polymorphic nodes.

I can do this (I think) if I accept that my nodes will read <A
xsi:type="B">, but this will be relatively unreadable. It's possible that I
can do this by overriding IXmlSerializable but I'm not sure if it will handle
deserialization with the inherited types.

I'm sure I can do this by using XmlReader and writing my own parser, but
that's not really the point of this Xml stuff, is it?

Any help or pointers much appreciated!

Iain
May 10 '07 #1
3 4913
Iain wrote:
suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}

public class C:A
{
public string nameA;

}

and I want to deserialise something like

<?xml?>
<A>
<Name>fred</Name>
<Contents>
<B>
<NameB>bert</NameB>
<Contents>
<A>
<Name>allison</Name>
</Contents>
</A>
<B>
<NameB>jim</NameB>
<Contents>..</Contents>
</Contents>
<A>

In short I want to represent a tree with polymorphic nodes.

I can do this (I think) if I accept that my nodes will read <A
xsi:type="B">, but this will be relatively unreadable. It's possible that I
can do this by overriding IXmlSerializable but I'm not sure if it will handle
deserialization with the inherited types.

I'm sure I can do this by using XmlReader and writing my own parser, but
that's not really the point of this Xml stuff, is it?

Any help or pointers much appreciated!

Iain
Hi, Iain
If the inherited type is serializable, there wouldn't be problems. Well
the XmlSerializer has certain limitations, but we don't live in a
perfect world ;)

There's a pretty good article at
http://www.codeproject.com/useritems...Serializer.asp

HTH,
Petar Atanasov
http://a-wake.net
May 11 '07 #2


"Petar Atanasov" wrote:
Iain wrote:
suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}
....

Thanks for your note, Petar. It turns out that the answer was (as you said)
quite straightforward. A good nights sleep and some code tests found it.

As the code above stands it does not serialise the B class as a B but as a
<B xsi:type="B">

Equally if I have a <Bin some text xml it gets ignored by the
deserialisation.

However, if I change the A class definition to

public class A
{
[XmlInclude(typeof(A))]
[XmlInclude(typeof(B))]
List<AContents;
}

public class B
{
....
}

It just plain works!

Iain

May 11 '07 #3
Iain wrote:
>
"Petar Atanasov" wrote:
>Iain wrote:
>>suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}
...

Thanks for your note, Petar. It turns out that the answer was (as you said)
quite straightforward. A good nights sleep and some code tests found it.

As the code above stands it does not serialise the B class as a B but as a
<B xsi:type="B">

Equally if I have a <Bin some text xml it gets ignored by the
deserialisation.

However, if I change the A class definition to

public class A
{
[XmlInclude(typeof(A))]
[XmlInclude(typeof(B))]
List<AContents;
}

public class B
{
...
}

It just plain works!

Iain
yeah :) good sleep always helps ;)

Regards,
Petar Atanasov
http://a-wake.net
May 11 '07 #4

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

Similar topics

0
1116
by: Kevin Schneider | last post by:
I am trying to use a web service written in Java but I get the following error message (the names have been changed to protect the innocent): "Member 'ChildClassVO.lastUpdateDate' hides...
1
1856
by: Glenn Wilson | last post by:
In the current project that I am planning i am thinking of serializing the objects to save as binary files. The question I have is with inheriting classes. If I have 1 base class and 2 classes...
2
5064
by: tony lock | last post by:
I have a class inherited from Control, which I want to serialize, since Control is not Serializable, I have had to implement ISerializable. This works but I now want to inherit this base class into...
12
3762
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
0
1287
by: cnSoftware | last post by:
I have two classes as below: public class BaseClass { public BaseClass() { } public BaseClass(string name) { this._name = name; }
4
1980
by: Mihai | last post by:
Hi ! I use VB.Net 2005. I created an inherited user control based on a previosly created user control.Everything is Ok . I can run this control with f5, everything is fine. When I close Visual...
12
2058
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
0
6586
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
8
2848
by: chr | last post by:
Hi I refer to an archived discussion titled inherited classes and xml.serialization I seem to have the same situation, i have two classes B and C inheriting class A, class A includes both types...
0
7124
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
6998
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
7200
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...
1
6884
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
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4586
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...
0
3090
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.