I get this error The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
Unexpected type 'A' with data contract name 'A: http://schemas.datacontract.org/2004/07/' . If you are using DataContractSerializer, try using DataContractResolver or add types not known statically to the list of known types (for example, using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer).
when serializing an object with a property that can be 2 types A or B.
xsd.exe generated the object like this:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class Doc
{
[XmlElement("A", typeof(A))]
[XmlElement("B", typeof(B))]
[System.Xml.Serialization.XmlTextAttribute(typeof(string))]
public object Item { get; set; }
}
Type A and B are defined too, just normal classes with 1 property.
and I initialize the object like this
Doc d = new Doc();
var i = new A
{
Id = 1,
};
doc.Item = i;
which compiles fine, but when it tries to serialize it throws the error mentioned above
XmlSerializerattributes but your exception was generated byDataContractSerializer, a completely different serializer that uses different attributes. You need to usesvcutil.exeto generate classes for it. - dbc