I've got this narrowed down a bit more. The error occurs when I have a class that contains an array of base class objects, and I use the XmlElement attribute on the array, e.g.:
[Serializable]
class MyClass
{
[XmlElement(ElementName = "BaseObject")]
public Base[] BaseObjects;
}
The problem only occurs if Base has some derived classes, e.g.
[Serializable]
[XmlInclude(typeof(Derived))]
class Base
{
}
If I remove the XmlElement from the BaseObjects field, everything works fine.
At the XSD level, the problem form is:
<xs:complexType name="MyClass">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="BaseObject" type="tns:Base" />
</xs:sequence>
</xs:complexType>
whereas the working form is:
<xs:complexType name="MyClass">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="BaseObjects" type="tns:ArrayOfBase" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfBase">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Base" nillable="true" type="tns:Base" />
</xs:sequence>
</xs:complexType>
It's not obvious to me why the latter works but the former doesn't. But now that I know what causes the problem, I can work around it.