Yes, we can prevent attribute serialization. Put the annotation [DataContract]in the class and [DataMember]only for the serialized attribute. if you want to skip an attribute when the value of this attribute is zero, put [DataMember(EmitDefaultValue = false)]in this attribute.
Example:
[DataContract]
public class MyClass
{
[DataMember]
public int Id{ get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string MessageBody { get; set; }
[DataMember(EmitDefaultValue = false)]
public DateTime SentOn { get; set; }
}
Note: SentOn will be serialized when it is non-zero, while others will be serialized in each condition.
source
share