简体   繁体   English

类型xxxx不期望使用xmlinclude或soapinclude

[英]type xxxx not expected use xmlinclude or soapinclude

I have a curious case of this serialization issue - which has been asked many times on this site and I have gone through a few of these questions and tried the usual items to no avail: 对于这个序列化问题,我有一个很好奇的案例-在该站点上已经问了很多遍了,我经历了以下几个问题,并尝试了平常的项目,但无济于事:

  • Add [XmlInclude] to the class throwing the error 将[XmlInclude]添加到引发错误的类中
  • Remove namespaces 删除名称空间
  • Add a different namespace to each class 向每个类添加不同的名称空间

To explain further, I have provided a simplified version of my code below. 为了进一步解释,我在下面提供了代码的简化版本。 Essentially I am using a WebServiceHost object to run a RESTful service and one of my endpoints returns an object serialized as XML (I have annotated the object with [DataContract] and [DataMember] attributes). 本质上,我使用WebServiceHost对象运行RESTful服务,并且我的一个端点返回一个序列化为XML的对象(我已经用[DataContract][DataMember]属性注释了该对象)。 This object contains a SerializableDictionary<string, object> ( here ) where the value has been typed as object . 该对象包含一个SerializableDictionary<string, object>此处 ),其中该值已被键入为object I believe this is why it is failing: 我相信这就是失败的原因:

  • Works fine when the value is assigned a primitive 将值分配为基元时,效果很好
  • When I assign a custom object to the KV pair V, I get the unexpected type exception probably because the Serializer does not know how to serilaize the object / some sort of namespacing issue 将自定义对象分配给KV对V时, 出现意外的类型异常,可能是因为序列化器不知道如何序列化对象/某种命名空间问题

Obviously, I am unable to annotate Object.cs with [XmlInclude] and because it is a service and I am not myself serializing I cannot using something like 显然,我无法使用[XmlInclude]注释Object.cs,并且由于它是一项服务,而且我自己也无法序列化,因此无法使用类似

new Serializer(typeof(...), new Type[] { ... }}

Any idea's of what I can do? 关于我能做什么的任何想法? I thought about not typing the dict value as object and rtaher comething more concrete but the problem is that this value can take primitives or cusotm types. 我考虑过不要将dict值作为对象输入,而更具体些,但问题是该值可以采用基元或cusotm类型。 Some code to explain the above: 一些代码可以解释以上内容:

Edit: Updated the code below to make it more clear 编辑:更新了下面的代码以使其更加清晰

[DataContract]
public class ResponseObject
{
    [DataMember(Name = "data")]
    public SerializableDictionary<string, object> Data { get;set; }

    public ResponseObject()
    {
        Data = new SerializableDictionary<string, object>();
    }
}

...

var d1 = new ResponseObject();
d1.Data.Add("some key", "some value"); //WORKS AND SERIALIZES PERFECLTY

var d2 = new ResponseObject();
d2.Data.Add("some other key", new SomeOtherObjecT());
var d3 = new ResponseObject();
d3.Data.Add("another key", d2);  //THIS THROWS THE UNEXPECTED TYPE ERROR WHEN SEIRLAIZING SomeOtherObject

Edit: The error is thrown in SerializableDictionary where it is attempting to serialize an object of type ResponseObject. 编辑:在尝试序列化类型为ResponseObject的对象的SerializableDictionary中引发该错误。 The two are in seperate projects - if that is significant? 两者在单独的项目中-是否有意义?

Normally, you should add an [XmlInclude] to the ResponseObject class. 通常,应将[XmlInclude]添加到ResponseObject类。 In this case, it doesn't work because of the SerializableDictionary that you're using. 在这种情况下,由于您使用的SerializableDictionary,它不起作用。 That class creates another XmlSerializer in its implementation, and therefore it doesn't care about your [XmlInclude]'s. 该类在其实现中创建另一个XmlSerializer,因此它并不关心您的[XmlInclude]。 Basically it just cannot handle your use case. 基本上,它只是无法处理您的用例。 You should switch from the XmlSerializer to the DataContractSerializer which handles the Dictionary class and supports the [KnownType] attribute to register additional types: http://pastebin.com/vGLSaxHF . 您应该从XmlSerializer切换到DataContractSerializer,后者处理Dictionary类并支持[KnownType]属性以注册其他类型: http : //pastebin.com/vGLSaxHF Also note that it's pointless to add [DataContract] and [DataMember] attributes in your current case because the XmlSerializer ignores those attributes, they are used by the DataContractSerializer only. 还要注意,在当前情况下添加[DataContract]和[DataMember]属性毫无意义,因为XmlSerializer会忽略这些属性,它们仅由DataContractSerializer使用。 Or if you're not sure how to change your serializer (I know I'm not) then you should either not be using a Dictionary or change the SerializableDictionary implementation to handle the dynamic object types that you want to use (find every line where it creates a new XmlSerializer). 或者,如果您不确定如何更改序列化程序(我知道我不是),那么您应该不使用Dictionary或更改SerializableDictionary实现来处理要使用的动态对象类型(在每行找到它会创建一个新的XmlSerializer)。 Or, as an alternative, define a base class for all your objects that you will ever put into the dictionary and do it like this: 或者,作为替代方案,为您将要放入字典中的所有对象定义一个基类,并像这样进行操作:

[XmlInclude(typeof(Class1), XmlInclude(typeof(Class2)), etc]
public class AbstractBase { }

public class Class1 : AbstractBase { ... }

public class Class2 : AbstractBase { ... }

public class BigClass {
    public SerializableDictionary<string, AbstractBase> Dictionary { get; set; }
}

This way, when the SerializableDictionary creates its own XmlSerializer, it will recognize the AbstractBase and from there, all of its descendants. 这样,当SerializableDictionary创建自己的XmlSerializer时,它将识别AbstractBase并从那里识别其所有后代。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “不希望使用RoleProxy类型。 使用XmlInclude或SoapInclude属性可以指定静态未知的类型。” NHibernate - “The type RoleProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.” NHibernate xxx类型不是预期的。 使用XmlInclude或SoapInclude属性来指定静态未知的类型 - The type xxx was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically Xml序列化异常:不需要类型UserQuery + SpecificContentItem。 使用XmlInclude或SoapInclude - Xml serialization Exception : The type UserQuery+SpecificContentItem was not expected. Use the XmlInclude or SoapInclude 调用Web服务时出现问题-使用XmlInclude或SoapInclude属性 - Issue calling a web service - Use the XmlInclude or SoapInclude attribute 使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型 - Use the XmlInclude or SoapInclude attribute to specify types that are not known statically WCF服务引发“未预期类型[…]。 即使已经使用XmlInclude,也要使用XmlInclude […]” - WCF service throws “The type […] was not expected. Use the XmlInclude […]” even if XmlInclude is already being used ASMX 服务错误 - 使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型 - ASMX Service Error - Use the XmlInclude or SoapInclude attribute to specify types that are not known statically 如何使用XmlInclude序列化IEnumerable - How to use XmlInclude to serialize IEnumerable 如何使用XmlInclude动态指定类型? - How to use XmlInclude to specify types dynamically? 没有[XMLInclude]或Type []构造函数的抽象类的序列化 - Serialization of an abstract class without [XMLInclude] or Type[] Constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM