Contact Yart for a relaxed conversation
Your Name * Message *
Your Email *  
Your Phone   
Our Phone:        03 8685 8718 (within Australia)
61 3 8685 8718 (outside Australia)
Contact Yart

Melbourne Content Management and Flash developers

 

for designers, agencies and your website

Resources / ASP NET / Serializing XML Lists of Abstract Types
Serializing XML Lists of Abstract Types

This is a common problem:


public class ConcreteClass : AbstractClass
{
  public string Name { get; set; }
}

public abstract class AbstractClass
{
  public DateTime DateTime { get; set; }
}

You won't be able to Serialize a list of this type:

      List<AbstractClass> list = new List<AbstractClass>();
      ConcreteClass c1 = new ConcreteClass();
      list.Add(c1);

      XmlSerializer serializer = new XmlSerializer(typeof(List<AbstractClass>));
      TextWriter textWriter = new StreamWriter(@"C:\list.xml");
      serializer.Serialize(textWriter, list);
      textWriter.Close();

you'll get the error

The type Serialize.ConcreteClass was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

I found the solution on StackOverflow and have created a project that shows you how to use it.

The latest at Yart
Search: