There was an error generating the XML document. ---> System.InvalidOperationException: The type DowEntity.Mark was not expected.
提示的错误如下:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type DowEntity.Mark was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_EntityBase(String n, String ns, EntityBase o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_ArrayOfEntityBase(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.MarksSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
此错误是在从mysql数据库中读取Marks时候出现的,返回的是Marks类型的对象,Marks是Mark的list集合。
错误的原因是 Mark和Marks的直接父类不一致:
/// <summary> /// 集合 /// </summary> [XmlInclude(typeof(EntitiesBase))] [Serializable] public class Marks:EntitiesBase { }
/// <summary> /// 标注 /// </summary> [XmlInclude(typeof(PowerBase))] [Serializable] public class Mark : PowerBase { ........... }
改正的方法是:将
Marks:EntitiesBase 改为 Marks:List<Mark>