Bug 3977 - Incorrect XML serialization of null for base64Binary and hexBinary types
Summary: Incorrect XML serialization of null for base64Binary and hexBinary types
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: 2.10.x
Hardware: Other Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-03-19 14:40 UTC by Matt Yellen
Modified: 2018-03-13 11:07 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Proposed patch to XmlCustomFormatter.cs (1.57 KB, patch)
2012-03-19 16:41 UTC, Matt Yellen

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Matt Yellen 2012-03-19 14:40:50 UTC
In System.Xml.Serialization, XmlCustomFormater.cs the method GenerateToXmlStringCore does not correctly handle a null value for base64Binary and hexBinary.  It returns String.Empty, but it should return null.  In other words those two cases should look like this:

case "base64Binary": return value + " == null ? null : Convert.ToBase64String (" + value + ")";
case "hexBinary": return value + " == null ? null : ToBinHexString (" + value + ")";

Also, for the sake of consistency, the method ToXmlString should be fixed as well; however in this case the null checks are unnecessary since there is already a check before entering the switch statement.
Comment 1 Matt Yellen 2012-03-19 16:41:38 UTC
Created attachment 1544 [details]
Proposed patch to XmlCustomFormatter.cs
Comment 2 Atsushi Eno 2013-08-09 05:08:08 UTC
I'm not sure why we need this. Does this differentiate the serialization results?

(No one is working on this namespace now so no one can guess what it is for.)
Comment 3 Alex Solovyov 2014-07-28 11:08:43 UTC
After creating and XML-serializing a new class with a null-able and not set property, output string will NOT contain this property TAG (it's correct behavior) and sometimes (NOT ALWAYS!, it's a function of time, hard to say who is blamable for this GC??? and when it can happen) it contains just closed tag <myproperty /> - this is not correct behavior. After such class de-serialization this property value will be equal to String.Empty (if property of type String) instead of NULL.
Comment 4 Marek Safar 2018-03-13 11:07:19 UTC
We have not received the requested information. If you are still experiencing this issue please provide all the requested information and reopen the bug report.

Thank you!