InvalidOperationException When XML Serializing Inherited Class
Posted
by Nick
on Stack Overflow
See other posts from Stack Overflow
or by Nick
Published on 2009-10-12T17:09:33Z
Indexed on
2010/03/13
14:55 UTC
Read the original article
Hit count: 327
I am having an issue serializing a c# class to an XML file that has a base class... here is a simple example:
namespace Domain
{
[Serializable]
public class ClassA
{
public virtual int MyProperty
{
get; set;
}
}
}
namespace Derived
{
public class ClassA : Domain.ClassA
{
public override int MyProperty
{
get { return 1; } set { /* Do Nothing */ }
}
}
}
When I attempt to serialize an instance of Derived.ClassA, I receive the following exception:
InvalidOperationException(Types 'Domain.ClassA' and 'Derived.ClassA' both use the XML type name 'ClassA', from the namespace ". Use XML attributes to specify a unique XML name and/or namespace for the type.)
The problem is that I want to create a single base class that simply defines the structure of the XML file, and then allow anyone else to derive from that class to insert business rules, but that the formatting will come through from the base.
Is this possible, and if so, how do I attribute the base class to allow this?
© Stack Overflow or respective owner