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.

Comments

Popular posts from this blog

Sentinel One Strikes Again. No internet connection. Uninstall Sentinel One Agent.

A2 Hosting Let's Encrypt Can't Install Certificate on ASP.NET Core Application

NuGet Package Reference NU6105 Publish Error