all groups > dotnet xml > april 2005 >
You're in the

dotnet xml

group:

Dynamic XmlInclude declaration


Dynamic XmlInclude declaration yuriy_zubarev
4/27/2005 12:16:57 PM
dotnet xml: Greetings,

Simple scenario: I have objects that need to be persisted in XML. The
declaration is as follows:


public class Zoo
{
protected Animal[] animals;

[
XmlArray("animals"),
XmlArrayItem("animal", typeof (Animal))
]
public Animal[] Animals
{
get { return this.animals; }
set { this.animals = value; }
}

}

public abstract class Animal
{
}


One way to make it work with concrete implementations of Animal is to
do something like this:

[XmlInclude(typeof(Lion))]
[XmlInclude(typeof(Tiger))]
[XmlInclude(typeof(Bear))]
public abstract class Animal
{
}


But this is a HUGE problem. I cannot have abstract class referencing
classes that implement it. Neither can I have Zoo class referencing the
same concrete classes (it must only know about Animal). What's the way
around it? How do I make XmlSerializer recognizing all possible
concrete classes of Animal type in a dynamic fashion?

Regards.
Re: Dynamic XmlInclude declaration yuriy_zubarev
4/27/2005 1:55:39 PM
False alarm. It was easier than I thought - XmlSerializer constructor
can take an array of extra types.
AddThis Social Bookmark Button