Can I configure the DataContractSerializer to not create optional (i.e. Nullable<> and List<>) elements in output XML?

后端 未结 2 1552
逝去的感伤
逝去的感伤 2021-02-07 15:26

I am using the new .NET 3.0 DataContractSerializer. I have both Nullable<> and List<> objects I am going to serialize. Example:

[DataContract(Na         


        
2条回答
  •  悲&欢浪女
    2021-02-07 15:39

    Mark the field with

       [DataMember(EmitDefaultValue=false)]
    

    That will work for at the least the nullable value type case. For the List case you may need to defer creation of the list until it is needed, or else null the member if it is empty before serialization.

提交回复
热议问题