Bug 35324 - DataContractSerializer.ReadObject(XMLReader) deserialization throwing exception on valid implementation
Summary: DataContractSerializer.ReadObject(XMLReader) deserialization throwing excepti...
Status: RESOLVED NORESPONSE
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.1
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2015-10-28 06:09 UTC by Bart
Modified: 2017-06-28 11:45 UTC (History)
8 users (show)

Tags: BZCU
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 Developer Community or GitHub 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 NORESPONSE

Description Bart 2015-10-28 06:09:57 UTC
OS: 			Windows 8.1
Visual Studio:		2012 Update 5
Xamarin:		3.11.590.0
Xamarin.Android:	5.1.3.1
Xamarin.iOS:		8.10.1.0
Ocurring at:		Android (iOS possibly, un-tested)

I am deserializing a not 'very' complex XML which is completely valid and deserializes without any problem in a regular .Net application. The following XML is simplified but is sufficient to trigger the problem:

<Boxes>
 <Entry>
  <Id>1</Id>
  <Title>B1</Title>
  <Tickets>
   <Entry>
    <Id>1</Id>
    <Title>Sample 1</Title>
    <Note>Note 1</Note>
    <Order>1</Order>
   </Entry>
   <Entry>
    <Id>2</Id>
    <Title>Sample 2</Title>
    <Note>Note 2</Note>
    <Order>2</Order>
   </Entry>
  </Tickets>
 </Entry>
 <Entry>
  <Id>2</Id>
  <Title>B2</Title>
  <Tickets>
   <Entry>
    <Id>3</Id>
    <Title>Sample 3</Title>
    <Note>Note 3</Note>
    <Order>3</Order>
   </Entry>
   <Entry>
    <Id>4</Id>
    <Title>Sample 4</Title>
    <Note>Note 4</Note>
    <Order>4</Order>
   </Entry>
  </Tickets>
 </Entry>
</Boxes>



The classes used to deserialize to:

		[CollectionDataContract(Name = "Boxes", Namespace = "")]
		private class ListOfTickets : List<TicketBoxData>
		{
		}

		[DataContract(Name = "Entry", Namespace = "")]
		private class TicketBoxData
		{
			[DataMember(Order = 1)]
			public int Id= 0;
			[DataMember(Order = 2)]
			public string Title = "";
			[DataMember(Order = 3)]                       // Commenting this will make it work..
			public List<TicketData> Tickets { get; set; } // Commenting this will make it work..
		}

		[DataContract(Name = "Entry", Namespace = "")]
		public class TicketData
		{
			[DataMember(Order = 1)]
			public int Id = 0;
			[DataMember(Order = 2)]
			public string Title = "";
			[DataMember(Order = 3)]
			public string Note = "";
			[DataMember(Order = 4)]
			public int Order = 0;
		}

This is not deserializable by the DataContractSerializer.ReadObject(XmlReader):

static public T Deserialize<T>(string xml)
		{
			System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
			using (var stringReader = new System.IO.StringReader(xml))
			{
				return ((T)(serializer.ReadObject(System.Xml.XmlReader.Create(stringReader))));
			}
		}



It throws the following exception and the problem seems to be the second time the 'Entry' is being used at a different level with a different class. The entry at Boxes-level needs to be deserialized in TicketBoxData object. The Entry at Tickets-level needs to be deserialized in a TicketData object. The current implementation in mono is deserializing every element called 'Entry' to TicketBoxData which is wrong.

{System.ArgumentException: The value "AnnStore_KitchenClient_Mobile.Managers.BusinessModelController+TicketBoxData" is not of type "AnnStore_KitchenClient_Mobile.Managers.BusinessModelController+TicketData" and cannot be used in this generic collection.
Parameter name: value
  at System.ThrowHelper.ThrowWrongValueTypeArgumentException (System.Object value, System.Type targetType) [0x00000] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/external/referencesource/mscorlib/system/throwhelper.cs:66 
  at System.Collections.Generic.List`1[AnnStore_KitchenClient_Mobile.Managers.BusinessModelController+TicketData].System.Collections.IList.Add (System.Object item) [0x0001a] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/external/referencesource/mscorlib/system/collections/generic/list.cs:262 
  at System.Runtime.Serialization.CollectionTypeMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id) [0x00083] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:669 
  at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x0003a] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:263 
  at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x000d3] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:251 
  at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00302] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:193 
  at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id, Boolean empty) [0x0021d] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:346 
  at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id) [0x00000] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:291 
  at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x0003a] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:263 
  at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x000d3] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:251 
  at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00302] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:193 
  at System.Runtime.Serialization.CollectionTypeMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id) [0x0006a] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:667 
  at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x0003a] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:263 
  at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x000d3] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:251 
  at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00302] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:193 
  at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Xml.XmlReader reader, System.Type declaredType, System.Runtime.Serialization.KnownTypeCollection knownTypes, IDataContractSurrogate surrogate, System.Runtime.Serialization.DataContractResolver resolver, System.Runtime.Serialization.DataContractResolver defaultResolver, System.String name, System.String ns, Boolean verifyObjectName) [0x0007e] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:64 
  at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x00024] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:355 
  at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlObjectSerializer.cs:76 
  at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlReader reader) [0x00000] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/mono/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:340 
  at Annoncer.Tools.Deserialize[ListOfTickets] (System.String xml) [0x00019] in d:\Development\AnnStore_KitchenClient_Mobile\AnnStore_KitchenClient_Mobile\AnnStore_KitchenClient_Mobile\AnnCommon_Library\AnnStoreClient_Mobile_Tools.cs:142 
  at AnnStore_KitchenClient_Mobile.Managers.BusinessModelController._ExtractDataFromResponse[ListOfTickets] (IMessage response) [0x0005d] in d:\Development\AnnStore_KitchenClient_Mobile\AnnStore_KitchenClient_Mobile\AnnStore_KitchenClient_Mobile\Managers\AnnStoreClient_Mobile_BusinessModelController.cs:173 }
Comment 1 Bart 2015-10-28 06:15:07 UTC
Function called to reproduce: 
SomeClass.Deserialize<ListOfTickets>(XmlFromTheDescription);
Comment 2 Ashley Gazich [MSFT] 2015-11-10 17:48:40 UTC
@Bart

Thank you for submitting this report. When you have a chance, would you please attach a runnable test case demonstrating this issue? We would like to ensure this reproduction is exactly what you are experiencing.
Comment 3 Bart 2015-12-02 10:57:35 UTC
I will make some time and include a project soon. Thanks for the response.
Comment 4 Adam Hartley [MSFT] 2017-06-28 11:45:01 UTC
Because we have not received a reply to our request for more information we are closing this issue. If you are still encountering this issue, please reopen the ticket with the requested information. Thanks!