suppose i am trying to deserialize xml to my class and if any value is null or empty for decimal or datetime then how to handle the null.
[XmlElement(ElementName = "Salary" , typeof(double))]
public string Salary { get; set; }
[XmlElement(ElementName = "BirthDate" , typeof(DateTime))]
public string Phone { get; set; }
suppose if BirthDate or Salary is null or empty in xml then how to handle it at the time of deserialization. need solution. thanks.
In an XML document, the usual way to represent a null value is to leave the element or attribute empty. Some business messages use a special value to represent a null value: <price>-999</price> . This style of null representation is supported by the DFDL and MRM parsers.
Empty XML ElementsAn element with no content is said to be empty. The two forms produce identical results in XML software (Readers, Parsers, Browsers). Empty elements can have attributes.
How can I set a field as null using the SOAP API? Notice the xsi:nil="true" on the <ns2:DataValue> tag. This tells the SOAP envelope to make this field as "NULL". Now the SOAP request should process without issue.
In your message action (e.g. Publish) right click the "text (String) {XML}" node and select "Properties". Clear the check box "Send NULL values". Your message action should now include an empty string as the node value.
You have two options as specified here in XmlSerializer Class
Specify a System.ComponentModel.DefaultValueAttribute to specify the default value
[System.ComponentModel.DefaultValueAttribute ("0")]
[XmlElement(ElementName = "Salary" , typeof(double))]
public string Salary { get; set; }
[System.ComponentModel.DefaultValueAttribute ("02-May-2011")]
[XmlElement(ElementName = "BirthDate" , typeof(datetime))]
public string Phone { get; set; }
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 or not to generate the XML element named "MyFirstName".
Make use of Nullabe type will resolve issue easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With