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

Xml Serialization problem...

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.

-F
Nov 11 '05 #1
1 3309
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

"CodeMonkey" <my********@blueyonder.co.uk> wrote in message
news:6b**************************@posting.google.c om...
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.

-F

Nov 11 '05 #2

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

Similar topics

4
by: David K | last post by:
we are having a problem when trying to serilalize and deserialize DataTable When replacing the container to be ListArray or HashTable everything works fine The code is very straight forward (...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
0
by: ktn | last post by:
Hi all, I'm a .NET beginner and I've got a problem on a program where I try to do an XML serialization. I get the following error : "An unmanaged exception of type...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
2
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
2
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...
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
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
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,...

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.