I've encountered a situation where XmlSerializer can't deserialize back its own output. The data structure basically consists of a root class Project containing a ProjectItems property (of a custom collection type) holding individual items. Some items may contain nested items recursively.
public class Project
{
[XmlElement("ProjectItem")]
public ProjectItemCollection { get; set; }
}
The XML produced by XmlSerializer looks like this:
<Project>
<ProjectItem xsi:type="ContentOrganizationPlanProjectItem">
<ProjectItem xsi:type="FolderProjectItem">
</ProjectItem>
<ProjectItem xsi:type="FolderProjectItem">
</ProjectItem>
</ProjectItem>
</Project>
Note: All unimportant stuff is removed from the code examples.
Originally I had the ProjectItem class decorated with XmlInclude attributes to cover all the various types of items that may occur in the data structure. That worked fine.
However, as it needs to be extensible from independent assemblies, I had to replace these attributes with a dynamically constructed array of all possible types passed to XmlSerializer constructor's extraTypes parameter using this SO answer.
Again—serialization works just fine. The problem is that when I try to deserialize it back, XmlSerializer throws an InvalidCastException saying:
Unable to cast object of type 'System.Xml.XmlNode[]' to type 'Whatever.ProjectItem'.
How do I make XmlSerializer deserialize its own output it this case?
Side note: I can't withstand dancing the XmlSerializer bullets anymore—Trinity, help!