I’m using DataContractSerializer for xml serialization. While It tries to serialize SubClass, throws the below error
'SubClass:http://schemas.datacontract.org/2004/07/SubClass ' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer
So I have added “SubClass” in KnownTypeAttribute to resolve this. But it does not work.
I have visited the below article for this issue, "Type not expected", using DataContractSerializer - but it's just a simple class, no funny stuff?
Please anyone let me know how to resolve this?
[DataContract(Name = "BaseClass"]
[KnownType("Types")]
public class BaseClass
{
[DataMember]
public bool IsSerialized{get;set;}
public static Type[] Types()
{
return MainClass.Types();
}
}
[DataContract(Name = "SubClass"]
public class SubClass:BaseClass //Getting error for this class
{
[DataMember]
public string Format {get;set;}
}
public class MainClass
{
public MainClass()
{
var ser = new DataContractSerializer(typeof(BaseClass));
MemoryStream stream = new MemoryStream();
ser.WriteObject(stream,new BaseClass());
}
public static Type[] Types()
{
return new Type[]{typeof(BaseClass),typeof(SubClass)};
}
}