Serializing a C# decimal with XmlSerializer bug

C#, XML Add comments

The .NET Framework 1.0 has a bug when serializing extremely small and extremely large decimals using XmlSerializer: the resulting values do not conform to the XML Schema decimal data type. Specifically, the serializer occasionally outputs the decimal in scientific notation. This also affects the decimal.ToString() method. This bug was fixed with the .NET Framework 1.1.

For example:

.NET Framework 1.0: Console.WriteLine(0.00000000000000000001M.ToString()) prints out 1E-20.
.NET Framework 1.1: Console.WriteLine(0.00000000000000000001M.ToString()) prints out 0.00000000000000000001.

To require the use of the 1.1 Framework, add the following stanza to your app.config:

<startup>
    <supportedRuntime version="v1.1.4322" />
    <requiredRuntime version="v1.1.4322" />
</startup>

This bug can be somewhat tricky to find, as the vast majority of the time decimals will be serialized correctly.

Comments are closed.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in