本文介绍了我想为XML序列化一个对象得到一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.InvalidOperationException:有出错生成XML文档。 ---> System.InvalidOperationException:是没有预料到的类型ProfileChulbul。使用XmlInclude或SoapInclude属性来指定未静态已知类型。   在System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(字符串名称,字符串NS,对象o,布尔xsiType)   在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterProfileDefinitionExportHolder.Write1_Object(String N,字符串NS,对象o,布尔ISNULLABLE,布尔needType)

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type ProfileChulbul was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterProfileDefinitionExportHolder.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)

如果你看到'ProfileChulbul'是一个对象,我想序列化

if you see 'ProfileChulbul' is an object, I am trying to serialize

任何想法,为什么这样呢?

Any Idea why so?

感谢名单

推荐答案

这会发生,当你序列化的类型有一个不是静态已知序列化实例的类型的属性。例如,如果键入 ProfileChulbul 有一个基类,这是它是如何从你什么序列化引用,序列化将不知道如何与它合作。

This happens when the type you're serializing has a property of a type that is not statically known to the serializer instance. For example, if the type ProfileChulbul has a base type, which is how it is referenced from what you're serializing, the serializer won't know how to work with it.

您有解决这个问题的几个选项:

You have a couple options for resolving this problem:

  • 添加 [XmlInclude(typeof运算(ProfileChulbul))] 属性(和其他属性的其他类型将被使用) ProfileChulbul 的基类

  • Add the [XmlInclude(typeof(ProfileChulbul))] attribute (and additional attributes for any other types that will be used) to ProfileChulbul's base class

修改您使用序列化使用仿制药,而不是对象的类

Modify the class you use for serialization to use generics instead of Object

的typeof(ProfileChulbul)(以及任何其他类型将被使用)到串行构造函数在运行时,像这样:

Pass typeof(ProfileChulbul) (and any other types that will be used) into the serializer constructor at runtime, like so:

VAR knownTypes =新类型[] {typeof运算(ProfileChulbul)的typeof(ProfileSomethingElse)};

无功序列化=新的XmlSerializer(typeof运算(TheSerializableType),knownTypes);

这篇关于我想为XML序列化一个对象得到一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 00:20