简体   繁体   中英

serialization DataContract and NULL datamember

I am having a problem serializing null value. actually I don't want to include it at all.

[DataContract]
public class SearchItem{
   public SearchCriteria()
    {
        IsActive = true;
        IsFiltered = true;
    }

    [DataMember(Order=01)]
    public bool? IsActive { get; set; }
    [DataMember(Order=02)]
    public bool IsPriceFiltered { get; set; }

}

and when I want to instanciate like

 new SearchCriteria(){
 IsActive = null, 
}

it still does serialize IsActive property.

any suggestions how to overcome this problem?

solutions like create as enum or instantiate with other constructor NOT a options for me.

You can use EmitDefaultValue .

[DataMember(Order=01, EmitDefaultValue=false)]
public bool? IsActive { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM