Why does a Web Service DataMember's Specified Attribute need to be set for int and Data but not for String

前端 未结 3 1917
甜味超标
甜味超标 2020-12-09 06:24

I have created a web service via WCF. Then I exposed it as a web service to make it usable with a .NET 2.0 application. I created some DataContract with DataMember that coul

相关标签:
3条回答
  • 2020-12-09 07:04

    Also you can use [DataMember(isRequired=True)]

    0 讨论(0)
  • 2020-12-09 07:17

    You could read the explanation here.

    Quote from XmlSerializer:

    If a schema includes an element that is optional (minOccurs = '0'), or if the schema includes a default value, you have two options. One option is to use System.ComponentModel.DefaultValueAttribute to specify the default value, as shown in the following code. Another option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of propertyNameSpecified. For example, if there is a field named "MyFirstName" you would also create a field named "MyFirstNameSpecified" that instructs the XmlSerializer whether to generate the XML element named "MyFirstName".

    The only acceptable for me workaround I've come so far is to use XmlSerializer instead of DataContractSerializer by using XmlSerializerFormatAttribute.

    0 讨论(0)
  • 2020-12-09 07:20

    The default parameters for the DataMember attribute are:

    bool EmitDefaultValue (default true)
    bool IsRequired (default false)
    

    If the property you are exposing is a non-nullable value type you should use:

    [DataMember(IsRequired = true)]
    public int InvoiceNo;
    
    0 讨论(0)
提交回复
热议问题