将EmitDefaultValue设置为false将对WCF和c#中的int、long、float和DateTime起作

本文关键字:EmitDefaultValue int long float 起作 DateTime 中的 false 将对 WCF 设置 | 更新日期: 2023-09-27 18:15:52

EmitDefaultValue设置为false不适合int和其他数据类型,请给我建议。我想从我的xml中删除空值字段。它似乎只适用于字符串类型。

还有其他更好的方法来实现这一点吗?

请做必要的事。

DataContract

 [Serializable]
    [DataContract]
    public class AddressContract
    {
        private int addressTypeIdField = 0;
        private int parentAddressTypeIDField = 0;
        private string postCodeField = null;
        private string phoneFlagField = null;
        private string emailFlagField = null;
        private float geoCodeLattitudeField = 0.0F;
        private float geoCodeLongitudeField = 0.0F;
        [DataMember(EmitDefaultValue = false)]
        public int AddressTypeId
        {
            get
            {
                return this.addressTypeIdField;
            }
            set
            {
                this.addressTypeIdField = value;
            }
        }
        [DataMember(EmitDefaultValue = false)]
        public int ParentAddressTypeID
        {
            get
            {
                return this.parentAddressTypeIDField;
            }
            set
            {
                this.parentAddressTypeIDField = value;
            }
        }
        [DataMember(EmitDefaultValue = false)]
        public string PostCode
        {
            get
            {
                return this.postCodeField;
            }
            set
            {
                this.postCodeField = value;
            }
        }
        [DataMember(EmitDefaultValue = false)]
        public string PhoneFlag
        {
            get
            {
                return this.phoneFlagField;
            }
            set
            {
                this.phoneFlagField = value;
            }
        }
        [DataMember(EmitDefaultValue = false)]
        public string EmailFlag
        {
            get
            {
                return this.emailFlagField;
            }
            set
            {
                this.emailFlagField = value;
            }
        }
        [DataMember(EmitDefaultValue = false)]
        public float GeoCodeLattitude
        {
            get
            {
                return this.geoCodeLattitudeField;
            }
            set
            {
                this.geoCodeLattitudeField = value;
            }
        }
        [DataMember(EmitDefaultValue = false)]
        public float GeoCodeLongitude
        {
            get
            {
                return this.geoCodeLongitudeField;
            }
            set
            {
                this.geoCodeLongitudeField = value;
            }
        }
    }

将EmitDefaultValue设置为false将对WCF和c#中的int、long、float和DateTime起作

按此链接

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.emitdefaultvalue.aspx

可以肯定的是它也可以处理int值