Il seguente errore:
{System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type xxxxxxx.Task was not expected. Use the XmlInclude attribute to specify types that are not known statically.
compare quando si cerca di serializzare in C# un oggetto che eredita da un altro:
C#
public class TaskBase
{
  [XmlElement]
  public Guid ID { get; set; }
  ...
}

[XmlRoot("TaskSummary")]
public class TaskSummary : TaskBase
{
  [XmlElement]
  public List<TaskBase> ChildrenTasks { get; set; }
  ...
}

[XmlRoot("Task")]
public class Task : TaskBase
{
  ...
}

class Program
{
  static void Main(string[] args)
  {
    TaskSummary ts = GetTaskSummary();

    XmlSerializer serializer = new XmlSerializer(ts.GetType());
    XmlWriterSettings ws = new XmlWriterSettings();
    ws.Indent = true;
    StringBuilder output = new StringBuilder();
    XmlWriter xmlWriter = XmlWriter.Create(output, ws);
    serializer.Serialize(xmlWriter, project);
    return output.ToString();
}
In questo caso, nel main, serializzo un oggetto ts di tipo TaskSummary ottenendo un eccezione.

Per completare la serializzazione è necessario decorare la classe TaskBase con l'attributo XmlInclude:
C#
[XmlInclude(typeof(Task))]
[XmlInclude(typeof(TaskSummary))]
public class TaskBase
{
  [XmlElement]
  public Guid ID { get; set; }
  ...
}
indicando tutte le clasi che derivano da essa (Task e TaskSummary) coinvolte nella serializzazione.
Potrebbe interessarti anche: