WCF DataContract deserialization problem with empty values

Let's say I have something like:

[DataContract(Namespace="http://bla.bla")]
public class MyClass {
    [DataMember] public long ResponseCode { get; set; }
    [DataMember] public long Fee { get; set; }
}

and the following comes from the channel:

<ns0:MyResult>
    <ns2:ResponseCode xmlns:ns2="http://bla.bla">101</ns2:ResponseCode>
    <ns2:Fee xmlns:ns2="http://bla.bla"></ns2:Fee>
</ns0:MyResult>

I get an error message:

----> System.Xml.XmlException: value '' cannot be parsed as type 'Int64'. ----> System.FormatException: the input line was not in the correct format.

I do not understand why. The default value of the IsRequiredparameter IsRequired DataContractis equal false, so I expect it to be deserialized without errors and initialize the missing value with default values ​​for the type (zero). What am I missing?

+3
source share
1 answer

from - http://msdn.microsoft.com/en-us/library/aa347792.aspx

IsRequired

Data Contract Versioning, DataMemberAttribute IsRequired ( false). , , . IsRequired true, ( , ), EmitDefaultValue false (, , ), , . ( ) , SerializationException.

<

XML <ns2:Fee>,

<ns0:MyResult>
    <ns2:ResponseCode xmlns:ns2="http://bla.bla">101</ns2:ResponseCode>
</ns0:MyResult>

, . WCF int .

, - i: nil = "true" -

<MyParentElement xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<MyElement i:nil="true"></MyElement>
</MyParentElement>

. , . , .

+3

Source: https://habr.com/ru/post/1780417/


All Articles