all groups > dotnet xml > july 2003 >
You're in the

dotnet xml

group:

Xml Serialization problem...


Xml Serialization problem... myspammail NO[at]SPAM blueyonder.co.uk
7/11/2003 2:30:30 AM
dotnet xml:
Hi,

I am trying to use xml serialization to simplify load/save functions
in some classes i have created and am hitting a few problems. Any help
to either would be most appreciated.

I have created a MasterShape class which contains a name, and a
'Shapes' class object, which itself uses the ICollection interface to
provide a class which is basically an array of shapes i derive from an
abstract base class AbShape, for example i derive Rectangle and Circle
from AbShape and wish to be able to store any combination of these
shapes..

The problems..

1)

As far as i know classes which implement the ICollection interface do
not get their properties serialized, hence why i have made the
collection class a member of a parent class.

However, when i output the xml i get something like this:

<MasterShape Name="TwoRectangles">
<Shapes>
<Rectangle position="0,0", dimensions="20,30" />
<Rectangle position="5,5", dimensions="10,20" />
</Shapes>
</MasterShape>

However, i do not want the <Shapes> element to appear... is there any
way to serialise the shape class (e.g. the objects contained in the
collection) without creating the <Shapes> element?

Desired output:

<MasterShape Name="TwoRectangles">
<Rectangle position="0,0", dimensions="20,30" />
<Rectangle position="5,5", dimensions="10,20" />
</MasterShape>


2)

The other problem i have is that when the collection class contains an
abstract class which may contain Rectangle or Circle objects, it seems
to crash with 'error generating xml document'.

When the collection is set to be all of type Rectangle or Circle it
works...

Is there a way to allow derived classes to be stored and serialized
according to the properties of the concrete class?

*whew*

Any help would be much appreciated here. Cheers again.

Re: Xml Serialization problem... Christoph Schittko [MVP]
7/15/2003 9:07:26 PM
Dear CodeMonkey,

it's all possible ;) and these questions have been answered before in this
group. Check the Google archives for detailed answers [0] ;)

The short answers are:

1) Attach the XmlElement attribute to the field/proeperty of the MasterShape
type

2) pass the subclass Type objects to the XmlSerializer constructor or
attaching an XmlElement attribute to the MasterShape field/property for each
subtype might wokr as well.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0] http://groups.google.com

[quoted text, click to view]

AddThis Social Bookmark Button