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