Dynamic XmlInclude declaration

  • Thread starter Thread starter yuriy_zubarev
  • Start date Start date
Y

yuriy_zubarev

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.
 
False alarm. It was easier than I thought - XmlSerializer constructor
can take an array of extra types.
 
Back
Top