Bug 4507 - XElements cannot be serialized with XmlSerializer
Summary: XElements cannot be serialized with XmlSerializer
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML (show other bugs)
Version: 2.10.x
Hardware: PC Mac OS
: Normal enhancement
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-18 15:24 UTC by Alex Corrado
Modified: 2015-03-26 01:33 UTC (History)
4 users (show)

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


Attachments

Description Alex Corrado 2012-04-18 15:24:42 UTC
According to MSDN, XElements should be serializable by XmlSerializer. The example at http://msdn.microsoft.com/en-us/library/bb675182.aspx works on .NET. However, on Mono, I get an InvalidOperationException because XElement does not have a public parameterless constructor. It doesn't appear to have such a constructor on .NET either, so I don't know how they make it work.

Unhandled Exception: System.InvalidOperationException: There was an error reflecting type 'XElementNullContainer'. ---> System.InvalidOperationException: There was an error reflecting field 'member'. ---> System.InvalidOperationException: There was an error reflecting type 'System.Xml.Linq.XElement'. ---> System.InvalidOperationException: System.Xml.Linq.XElement cannot be serialized because it does not have a default public constructor
  at System.Xml.Serialization.ReflectionHelper.CheckSerializableType (System.Type type, Boolean allowPrivateConstructors) [0x000cd] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/ReflectionHelper.cs:101 
  at System.Xml.Serialization.XmlReflectionImporter.ImportXmlSerializableMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00030] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:673 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x000bb] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:226 
  --- End of inner exception stack trace ---
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00174] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:241 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0003c] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:196 
  at System.Xml.Serialization.XmlReflectionImporter.ImportElementInfo (System.Type cls, System.String defaultName, System.String defaultNamespace, System.Type defaultType, System.Xml.Serialization.XmlTypeMapMemberElement member, System.Xml.Serialization.XmlAttributes atts) [0x00196] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:988 
  at System.Xml.Serialization.XmlReflectionImporter.CreateMapMember (System.Type declaringType, System.Xml.Serialization.XmlReflectionMember rmember, System.String defaultNamespace) [0x004c2] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:935 
  at System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x001e7] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:373 
  --- End of inner exception stack trace ---
  at System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0020b] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:377 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0006e] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:221 
  --- End of inner exception stack trace ---
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00174] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:241 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0003c] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:196 
  at System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Xml.Serialization.XmlAttributeOverrides overrides, System.Type[] extraTypes, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00047] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:227 
  at System.Xml.Serialization.XmlSerializer..ctor (System.Type type) [0x00000] in <filename unknown>:0 
  at XLinqTest.Test[XElementNullContainer] (.XElementNullContainer obj) [0x00006] in /Users/alex/Projects/xmltest/xmltest/Main.cs:51 
  at XLinqTest.Main (System.String[] args) [0x00000] in /Users/alex/Projects/xmltest/xmltest/Main.cs:36 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: There was an error reflecting type 'XElementNullContainer'. ---> System.InvalidOperationException: There was an error reflecting field 'member'. ---> System.InvalidOperationException: There was an error reflecting type 'System.Xml.Linq.XElement'. ---> System.InvalidOperationException: System.Xml.Linq.XElement cannot be serialized because it does not have a default public constructor
  at System.Xml.Serialization.ReflectionHelper.CheckSerializableType (System.Type type, Boolean allowPrivateConstructors) [0x000cd] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/ReflectionHelper.cs:101 
  at System.Xml.Serialization.XmlReflectionImporter.ImportXmlSerializableMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00030] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:673 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x000bb] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:226 
  --- End of inner exception stack trace ---
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00174] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:241 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0003c] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:196 
  at System.Xml.Serialization.XmlReflectionImporter.ImportElementInfo (System.Type cls, System.String defaultName, System.String defaultNamespace, System.Type defaultType, System.Xml.Serialization.XmlTypeMapMemberElement member, System.Xml.Serialization.XmlAttributes atts) [0x00196] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:988 
  at System.Xml.Serialization.XmlReflectionImporter.CreateMapMember (System.Type declaringType, System.Xml.Serialization.XmlReflectionMember rmember, System.String defaultNamespace) [0x004c2] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:935 
  at System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x001e7] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:373 
  --- End of inner exception stack trace ---
  at System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0020b] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:377 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0006e] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:221 
  --- End of inner exception stack trace ---
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00174] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:241 
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0003c] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:196 
  at System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Xml.Serialization.XmlAttributeOverrides overrides, System.Type[] extraTypes, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00047] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:227 
  at System.Xml.Serialization.XmlSerializer..ctor (System.Type type) [0x00000] in <filename unknown>:0 
  at XLinqTest.Test[XElementNullContainer] (.XElementNullContainer obj) [0x00006] in /Users/alex/Projects/xmltest/xmltest/Main.cs:51 
  at XLinqTest.Main (System.String[] args) [0x00000] in /Users/alex/Projects/xmltest/xmltest/Main.cs:36
Comment 1 Miguel de Icaza [MSFT] 2012-04-23 23:52:38 UTC
We have not extended our serializer to support XElement, do not want to get your hopes high.   It will be a while.
Comment 2 Atsushi Eno 2015-03-17 06:28:34 UTC
XElement has been serializable (martin fixed the issue a while ago).
Comment 3 Rodrigo Amaro 2015-03-25 21:35:01 UTC
I've got this error when trying to consume a SOAP web service (.NET)

Here's the stack:

  at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00094] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/corlib/System/Activator.cs:326 
  at System.Runtime.Serialization.XmlSerializableMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:420 
  at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x000d3] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:252 
  at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00302] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:194 
  at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id, Boolean empty) [0x0021d] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:347 
  at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id) [0x00000] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:292 
  at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x0003a] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/SerializationMap.cs:264 
  at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x000d3] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:252 
  at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00302] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:194 
  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 /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlFormatterDeserializer.cs:65 
  at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x00024] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:363 
  at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlObjectSerializer.cs:76 
  at System.ServiceModel.Dispatcher.DataContractMessagesFormatter.ReadMessagePart (System.ServiceModel.Description.MessagePartDescription part, System.Xml.XmlDictionaryReader r) [0x00037] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.ServiceModel/System.ServiceModel.Dispatcher/BaseMessagesFormatter.cs:439 
  at System.ServiceModel.Dispatcher.DataContractMessagesFormatter.MessageToParts (System.ServiceModel.Description.MessageDescription md, System.ServiceModel.Channels.Message message) [0x00116] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.ServiceModel/System.ServiceModel.Dispatcher/BaseMessagesFormatter.cs:421 
  at System.ServiceModel.Dispatcher.BaseMessagesFormatter.DeserializeReply (System.ServiceModel.Channels.Message message, System.Object[] parameters) [0x0004d] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.ServiceModel/System.ServiceModel.Dispatcher/BaseMessagesFormatter.cs:272 
  at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply (System.ServiceModel.Channels.Message message, System.Object[] parameters) [0x00000] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.ServiceModel/System.ServiceModel.Dispatcher/BaseMessagesFormatter.cs:91 
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, Boolean isAsync, System.Object[]& parameters, System.ServiceModel.OperationContext context) [0x00284] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.ServiceModel/System.ServiceModel/ClientRuntimeChannel.cs:576 
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel.DoProcess (System.Reflection.MethodBase method, System.String operationName, Boolean isAsync, System.Object[]& parameters, System.ServiceModel.OperationContext context) [0x0003a] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.ServiceModel/System.ServiceModel/ClientRuntimeChannel.cs:501 
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Process (System.Reflection.MethodBase method, System.String operationName, Boolean isAsync, System.Object[]& parameters, System.ServiceModel.OperationContext context) [0x0000d] in /private/tmp/source-mono-mac-3.12.99-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.1/mcs/class/System.ServiceModel/System.ServiceModel/ClientRuntimeChannel.cs:482
Comment 4 Atsushi Eno 2015-03-26 01:33:11 UTC
Without any repro with stack trace that doesn't mention XElement (besides, it is from non-master version of mono), I think it's unrelated.

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