How to remove k__BackingField from json when Deserialize

.net, deserialization, json, serialization

Solution

Automatic Property syntax is actually not recommended if the class can be used in serialization. Reason being the backing field is generated by compiler which can be different each time code is compiled. This can cause incompatibility issues even if no change is made to the class (just recompiling the code).

I think applying DataMember attribute will fix the issue in this case. But I would recommend to use full property syntax, if the class needs to be used in serialization.

Problem

I am getting the k_BackingField in my returned json after serializing a xml file to a .net c# object. I've added the DataContract and the DataMember attribute to the .net c# object but then I get nothing on the json, client end. ``` [XmlRoot("person")] [Serializable] public class LinkedIn { [XmlElement("id")] public string ID { get; set; } [XmlElement("industry")] public string Industry { get; set; } [XmlElement("first-name")] public string FirstName { get; set; } [XmlElement("last-name")] public string LastName { get; set; } [XmlElement("headline")] } ``` Example of the returned json: ``` home: Object <FirstName>k__BackingField: "Storefront" <LastName>k__BackingField: "Doors" ```

Original source

Related problems