sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
steve's Avatar

Serialization of a derived class to the same element as the base class


Question posted by: steve (Guest) on November 11th, 2005 10:49 PM
I have the following two classes' the second derived from the first.
Each time 'myclass' is added it adds an element. The second class is
the same in, except it includes more methods.
What I want to be able to do is add the class element even if I use my
'wizclass'.

public class myclass {
{...}
}

public class wizclass : myclass{
{...}
}


Here is my serialization attribute, even if I cast 'wizclass' back to
'myclass' I still get an type exception.

[System.Xml.Serialization.XmlElementAttribute("class",
Type=typeof(myclass))]
public ArrayList Entities;
4 Answers Posted
Christoph Schittko [MVP]'s Avatar
Christoph Schittko [MVP] November 11th, 2005 10:49 PM
Guest - n/a Posts
#2: Re: Serialization of a derived class to the same element as the base class

You need to declare the wizclass type to occur in the array, just as you are
doing it ofor the myclass type. One way to do that is to atach a second
attribute like this:

[System.Xml.Serialization.XmlElementAttribute("class",
Type=typeof(myclass))]
[System.Xml.Serialization.XmlElementAttribute("class",
Type=typeof(wizclass))]
public ArrayList Entities;

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

"steve" <stephen.prescott@thermo.com> wrote in message
news:98180e91.0307170034.618c402b@posting.google.c om...[color=blue]
> I have the following two classes' the second derived from the first.
> Each time 'myclass' is added it adds an element. The second class is
> the same in, except it includes more methods.
> What I want to be able to do is add the class element even if I use my
> 'wizclass'.
>
> public class myclass {
> {...}
> }
>
> public class wizclass : myclass{
> {...}
> }
>
>
> Here is my serialization attribute, even if I cast 'wizclass' back to
> 'myclass' I still get an type exception.
>
> [System.Xml.Serialization.XmlElementAttribute("class",
> Type=typeof(myclass))]
> public ArrayList Entities;[/color]


stephen prescott's Avatar
stephen prescott November 11th, 2005 10:49 PM
Guest - n/a Posts
#3: Re: Serialization of a derived class to the same element as the base class

Thanks..

I did try this but I get the following error because the attribute and
namespace are not unique.

There was an error reflecting field 'Entities'. --->
System.InvalidOperationException: The XML element named 'class' from
namespace 'blah-de-blah' is already present in the current scope. Use
XML attributes to specify another XML name or namespace for the element.



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Christoph Schittko [MVP]'s Avatar
Christoph Schittko [MVP] November 11th, 2005 10:49 PM
Guest - n/a Posts
#4: Re: Serialization of a derived class to the same element as the base class

My apologies ... I think I didn't pay close enough attention.

You do need to specify a different name for the element you are writing out
when you do this via XmlElementAttributes, otherwise the XmlSerializer will
not know what the object type to deserialize to when it reads in the
document later.

What should work is to attach an XmlIncludeAttibute to he myclass type to
declare the wizclass type:

[XmlInclude( typeof( wizclass )) ]
public class myclass
{
....
}

the output will then contian an xsi:type attribute to store the type to
deserialize to. I hope that acceptable for you.
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"stephen prescott" <stephen.prescott@thermo.com> wrote in message
news:ezFcMSHTDHA.1920@TK2MSFTNGP11.phx.gbl...[color=blue]
> Thanks..
>
> I did try this but I get the following error because the attribute and
> namespace are not unique.
>
> There was an error reflecting field 'Entities'. --->
> System.InvalidOperationException: The XML element named 'class' from
> namespace 'blah-de-blah' is already present in the current scope. Use
> XML attributes to specify another XML name or namespace for the element.
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it![/color]


stephen prescott's Avatar
stephen prescott November 11th, 2005 10:49 PM
Guest - n/a Posts
#5: Re: Serialization of a derived class to the same element as the base class

This has really helped, thanks a lot.



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Not the answer you were looking for? Post your question . . .
196,805 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,805 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors