Bug 3258 - Serialize Nullable datetimeoffset
Summary: Serialize Nullable datetimeoffset
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: unspecified
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-02-07 17:59 UTC by James Clancey
Modified: 2012-06-18 23:37 UTC (History)
5 users (show)

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

code fragment to generate error (260 bytes, text/plain)
2012-03-04 17:42 UTC, Steve Meagher

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 James Clancey 2012-02-07 17:59:00 UTC
I stepped through the MonoTouch code and believe the issue is that MonoToch handles DateTimeOffset fine but not Nullable DateTimeOffset. 

In XmlFormatterSerializer.cs, the SerializePrimitive method is:

public void SerializePrimitive (Type type, object graph, QName qname)


string label;

if (TrySerializeAsReference (false, graph, out label))


if (label != null)

Writer.WriteAttributeString ("z", "Id", KnownTypeCollection.MSSimpleNamespace, label);

//	 writer.WriteStartAttribute ("type", XmlSchema.InstanceNamespace);

//	 writer.WriteQualifiedName (qname.Name, qname.Namespace);

//	 writer.WriteEndAttribute ();

// It is the only exceptional type that does not serialize to string but serializes into complex element.

if (type == typeof (DateTimeOffset)) {

var v = (DateTimeOffset) graph;

writer.WriteStartElement ("DateTime", KnownTypeCollection.DefaultClrNamespaceSystem);

SerializePrimitive (typeof (DateTime), DateTime.SpecifyKind (v.DateTime.Subtract (v.Offset), DateTimeKind.Utc), KnownTypeCollection.GetPredefinedTypeName (typeof (DateTime)));

writer.WriteEndElement ();

writer.WriteStartElement ("OffsetMinutes", KnownTypeCollection.DefaultClrNamespaceSystem);

SerializePrimitive (typeof (int), v.Offset.TotalMinutes, KnownTypeCollection.GetPredefinedTypeName (typeof (int)));

writer.WriteEndElement ();



writer.WriteString (KnownTypeCollection.PredefinedTypeObjectToString (graph));


Because there is only a check for typeof(DateTimeOffset), a nullable datetimeoffset fails the check and falls through to the 'else' which calls PredefinedTypeObjectToString which generates the exception: "Internal error: missing predefined type serialization for type " + type.FullName). If the check included nullable datetimeoffset, it should work, shouldn't it?
Comment 1 Miguel de Icaza [MSFT] 2012-02-26 12:27:34 UTC

You included the analysis, but not the original test case that you were using to diagnose this.

Would you mind including the relevant information?
Comment 2 Steve Meagher 2012-03-04 17:42:54 UTC
Created attachment 1460 [details]
code fragment to generate error

The attached file contains code that generates an error during the serialization.  If the nullable datetimeoffset is changed to a datetimeoffset, the error does not occur.  Please see bug description for more information.
Comment 3 Miguel de Icaza [MSFT] 2012-03-08 00:19:20 UTC
This looks like this one:


The problem is that the easy fix is actually wrong;   The real fix is being researched.
Comment 6 Miguel de Icaza [MSFT] 2012-06-18 21:47:15 UTC
Vinicius, did I apply this fix already?