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 (show other bugs)
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)

See Also:
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

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!

Note You need to log in before you can comment on or make changes to this bug.