Bug 1852 - XmlSerialization ignores ShouldSerialize* and *Specified
Summary: XmlSerialization ignores ShouldSerialize* and *Specified
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: 2.10.x
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
Depends on:
Reported: 2011-11-03 08:39 UTC by Anonymous
Modified: 2014-11-27 03:41 UTC (History)
11 users (show)

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

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 Anonymous 2011-11-03 08:39:43 UTC
Description of Problem:
I want to add conditional Property ignorance in serialization output. Usually (in .net 2.0+) I did this with extra public bool ShouldSerialize{PROPERTY_NAME} method or extra public bool {PROPERTY_NAME}Specified property. 

But Mono's XmlSerialization ignores ShouldSerialize* and *Specified.

To reproduce the problem you can try this class and code:

public sealed class Coords
        private bool _hasY;

        public float X { get; set; }

        public float Y { get; set; }
        public bool ShouldSerializeY() { return _hasY; }

        public float Z { get; set; }

        public Coords(bool hasY)
            _hasY = hasY;

        public Coords()
            : this(true)

// temp here is an instance of Coords, var temp = Coords(false);
var xml = string.Empty;
using (var stream = new MemoryStream())
        var serializer = new XmlSerializer(temp.GetType());
        serializer.Serialize(stream, temp);
        xml = Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Position);

Actual Results:
<Coords X="-2694.85718" Y="0" Z="3953.91724" />

Expected Results:
<Coords X="-2694.85718" Z="3953.91724" />

How often does this happen? 

Additional Information:
I've tested this on Debian Squeeze with mono 2.10.6 and 2.10.5, and Mac Os Lion with mono 2.10.6 — it fails.
On any .net (2.0, 3.5, 4.0 and 4.5 Preview) it works like expected.
Comment 1 baramuse 2012-09-26 18:50:29 UTC
As the bug is still not resolved, I can confirm it is still present in Mono 2.10.9, and Monotouch 6.0.1

Any update on this ?
Comment 2 Rafael Mueller 2013-11-26 07:06:23 UTC
It's currently working with the version below.

> mono -V

Mono JIT compiler version 3.3.0 (master/6cd4ddc)
Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           normal
        SIGSEGV:       normal
        Notification:  Thread + polling
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen
Comment 6 marcos.henrich 2014-09-05 09:19:20 UTC
Hi Sergey, Baramuse,

Thank you for the bug report.
The pull request for this issue can be found in the link below.
Comment 7 marcos.henrich 2014-09-23 04:10:27 UTC
Fixed in master 52cdea5c0ad5e4ee8ee218b0f90522645ec3ad8a.
Comment 8 Shruti 2014-11-27 03:41:53 UTC
I have checked this issue, It has been fixed.

I have created an Android Application and implemented code given in Comment(0). I debug this app and observed that When ‘True ‘is passed in coords, its returning Y result and When passed ‘False’ in coords, its not giving result of Y.

Screencast regarding same : http://www.screencast.com/t/IgTUeSl8

Environment Info :
=== Xamarin Studio ===

Version 5.7 (build 596)
Installation UUID: 67eaf3b4-f8a7-4ab4-a9df-3197350ca5dc
	Mono 3.12.0 ((detached/b75fa2b)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000046

=== Apple Developer Tools ===

Xcode 6.2 (6736)
Build 6C86e

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 880cc21
Build date: 2014-11-25 12:12:17-0500

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/ixamarin78/Desktop/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 507000596
Git revision: d996e9ba6874a0d64241e43e5e6b06322ce29c84
Build date: 2014-11-25 17:17:54-05
Xamarin addins: 8ca19707b41536a391f53364ee4ff9272711feb0

=== Operating System ===

Mac OS X 10.10.0
Darwin XamnewiMac.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64