2

I am exposing an entity over Web API and when the response is coming back as XML, It is getting serialized in a very not readable way.

I don't want to get rid of the [Serializable] attribute and I would prefer not to annotate the entity and its fields with [DataContract] and [DataMember] attributes.

I was able to solve the JSON serialization by ignoring the Serializable attribute. I want to basically be able to do the same thing for XML serialization.

Is there a way around this?

Sam
  • 875
  • 10
  • 22
  • "It is getting serialized in a very not readable way." do you have a class and xml example of what you mean? – sa_ddam213 Aug 29 '14 at 03:42
  • I think [this](https://stackoverflow.com/questions/29701891/k-backingfield-remove-in-c-sharp-seen-via-swashbuckle-swagger?answertab=active#tab-top) will solve your issue – Mohamed Emad Aug 30 '17 at 14:05

1 Answers1

0

the answer is write in this link but I will write it here again to see it

Add this to WebApiConfig.cs:

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
new DefaultContractResolver { IgnoreSerializableAttribute = true };

This resolves the issue for classes marked with [Serializable]. I also have intermittent problems even if no classes have that attribute, so I always use this setting now.

Mohamed Emad
  • 104
  • 1
  • 8