Type xxxx is not expected to use xmlinclude or soapinclude

I have a curious case of this serialization issue that has been asked repeatedly on this site, and I examined several of these questions and tried the usual elements to no avail:

  • Add [XmlInclude] to the error throwing class
  • Delete namespaces
  • Add a different namespace to each class

To clarify further, I have provided a simplified version of my code below. Essentially, I use the WebServiceHost object to start the RESTful service, and one of my endpoints returns an object serialized as XML (I annotated the object with the [DataContract] and [DataMember] ). This object contains a SerializableDictionary<string, object> ( here ), where the value was entered as object . I believe that is why it fails:

  • Works great when a primitive is assigned to this assignment.
  • When I assign a custom object to a KV V pair, I get an exception from an unexpected type, probably because the Serializer does not know how to serialize the object / some kind of namespace problem

Obviously, I cannot comment on Object.cs using [XmlInclude] and because it is a service, and I am not a serialization, I cannot use something like

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

Any idea what I can do? I was thinking about not introducing the dict value as an object, and the other is more specific, but the problem is that this value can take primitives or cusotm types. Some code to explain above:

Edit: Updated the code below to make it clearer

 [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: Error in SerializableDictionary, where it is trying to serialize an object of type ResponseObject. They are in separate projects - if this is important?

+6
source share
1 answer

Usually you should add [XmlInclude] to the ResponseObject class. In this case, this does not work due to the SerializableDictionary used. This class creates another XmlSerializer in its implementation, and therefore it does not care about your [XmlInclude]. Basically, it just can't handle your use case. You should switch from the XmlSerializer to the DataContractSerializer, which processes the Dictionary class and supports the [KnownType] attribute to register additional types: http://pastebin.com/vGLSaxHF . Also note that it is pointless to add the [DataContract] and [DataMember] attributes in your current case, because the XmlSerializer ignores these attributes, they are used only by the DataContractSerializer. Or, if you do not know how to change your serializer (I know that it is not), you should either not use the dictionary or change the implementation of SerializableDictionary to handle the types of dynamic objects that you want to use (find every line where it creates new XmlSerializer). Or, as an alternative, define a base class for all of your objects that you ever put in 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; } } 

Thus, when SerializableDictionary creates its own XmlSerializer, it recognizes AbstractBase and from there all its descendants.

+9
source

Source: https://habr.com/ru/post/958147/


All Articles