Bug 7613 - System.FormatException is thrown when deserializing XML member which maps to a nullable type (if XmlElement is used)
Summary: System.FormatException is thrown when deserializing XML member which maps to ...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: master
Hardware: PC All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-03 07:03 UTC by Andres G. Aragoneses
Modified: 2012-10-03 13:44 UTC (History)
1 user (show)

Tags:
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:
Status:
RESOLVED FIXED

Description Andres G. Aragoneses 2012-10-03 07:03:21 UTC
Xml deserialization fails if using an [XmlElement()] attribute on a member which is nullable, and the value is xsi:nil="true".

More clearly, the following 3 tests pass in .NET, but only 2 pass on Mono:

		public class Foo
		{
			public DateTime? Baz { get; set; }
		}

		[Test]
		public void CanDeserializeXsiNil()
		{
			var reader = new StringReader(
@"<Foo xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
	<Baz xsi:nil=""true"" />
</Foo>");

			using (var xmlReader = new XmlTextReader(reader))
			{
				var serializer = new XmlSerializer(typeof(Foo));
				var foo = (Foo)serializer.Deserialize(xmlReader);
				Assert.IsNull(foo.Baz);
			}
		}

		public class Bar
		{
			[XmlElement("baz")]
			public DateTime? Baz { get; set; }
		}

		[Test]
		public void CanDeserializeXsiNilToAPropertyWithXmlElementAttrib()
		{
			var reader = new StringReader(
@"<Bar xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
	<baz xsi:nil=""true"" />
</Bar>");

			using (var xmlReader = new XmlTextReader(reader))
			{
				var serializer = new XmlSerializer(typeof(Bar));
				var bar = (Bar)serializer.Deserialize(xmlReader);
				Assert.IsNull(bar.Baz);
			}
		}

		public class FooBar
		{
			[XmlElement("baz", IsNullable = true)]
			public DateTime? Baz { get; set; }
		}

		[Test]
		public void CanDeserializeXsiNilToAPropertyWithXmlElementAttribAndIsNullableTrue()
		{
			var reader = new StringReader(
@"<FooBar xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
	<baz xsi:nil=""true"" />
</FooBar>");

			using (var xmlReader = new XmlTextReader(reader))
			{
				var serializer = new XmlSerializer(typeof(FooBar));
				var foobar = (FooBar)serializer.Deserialize(xmlReader);
				Assert.IsNull(foobar.Baz);
			}
		}

The failure is:

MonoTests.System.XmlSerialization.DeserializationTests.CanDeserializeXsiNilToAPropertyWithXmlElementAttrib : System.FormatException : Invalid format string
at System.DateTime.ParseExact (System.String s, System.String[] formats, IFormatProvider provider, DateTimeStyles style) [0x0005e] in /home/dev/mono/mcs/class/corlib/System/DateTime.cs:1799
at System.Xml.XmlConvert.ToDateTime (System.String s, System.String[] formats, DateTimeStyles style) [0x00000] in /home/dev/mono/mcs/class/System.XML/System.Xml/XmlConvert.cs:407
at System.Xml.XmlConvert.ToDateTime (System.String s, XmlDateTimeSerializationMode dateTimeOption) [0x00035] in /home/dev/mono/mcs/class/System.XML/System.Xml/XmlConvert.cs:378
at System.Xml.Serialization.XmlCustomFormatter.FromXmlString (System.Xml.Serialization.TypeData type, System.String value) [0x001c7] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlCustomFormatter.cs:274
at System.Xml.Serialization.XmlSerializationReaderInterpreter.GetValueFromXmlString (System.String value, System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlTypeMapping typeMap) [0x0004c] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:665
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadPrimitiveValue (System.Xml.Serialization.XmlTypeMapElementInfo elem) [0x00063] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:653
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem) [0x0003f] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:627
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readBySoapOrder) [0x00840] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:476
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob) [0x00000] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:248
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType) [0x000df] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:238
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType) [0x00031] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:201
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) [0x00057] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:192
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00028] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:87
at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x0001c] in /home/dev/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:362





I've got a pull request to fix this, almost ready.
Comment 1 Andres G. Aragoneses 2012-10-03 07:11:08 UTC
https://github.com/mono/mono/pull/474
Comment 2 Andres G. Aragoneses 2012-10-03 13:44:01 UTC
PR merged, thanks Marek