Friday, May 5, 2017

XMLSerializer Anyone

I rewrote a project to utilize XMLSerializer instead of manually append xml tags. It works great until I encountered problem with backward compatibility on Boolean type. Our client is sending boolean value with first letter capitalized. "True" and "False" and apparently XMLSerializer were unable to deserialize the value as boolean. Because by convention, boolean in XML has to be all lower case. Searching online I found out that I need to do a small trick.

Instead of:

<XmlElement("isvalid")>
Public Property IsValid As Boolean

I have to rewrite it as:

<XmlElement("isvalid")>
Public Property IsValidString As String
   Get
      Return IsValid.ToString().ToLower()
   End Get
   Set
      Boolean.TryParse(value, IsValid)
   End Set
End Property

<XmlIgnore>
Public Property IsValid As Boolean

The modified version can handle both formats.

No comments:

Post a Comment