Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot cast XML to RealmObject #1913

Closed
Doxoh opened this issue Oct 17, 2019 · 3 comments · Fixed by #1915
Closed

Cannot cast XML to RealmObject #1913

Doxoh opened this issue Oct 17, 2019 · 3 comments · Fixed by #1915

Comments

@Doxoh
Copy link

Doxoh commented Oct 17, 2019

Hello,

i want to to XmlSerializer to get the XML as object.

I use this function.

            XmlRootAttribute xRoot = new XmlRootAttribute
            {
                ElementName = "telegram",
                IsNullable = true
            };
XmlSerializer ser = new XmlSerializer(typeof(Telegram), xRoot);

By calling the Constructor I get the following error.

The model Telegram is not an IEnumerable or has a IEnumerable in it

{System.InvalidOperationException: There was an error reflecting type 'BAAS.Model.Telegram'. ---> System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(Realms.Schema.Property) at all levels of their inheritance hierarchy. Realms.Schema.ObjectSchema does not implement Add(Realms.Schema.Property).
at System.Xml.Serialization.TypeData.get_ListItemType () [0x00209] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs:395
at System.Xml.Serialization.TypeData.get_ListItemTypeData () [0x00016] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs:336
at System.Xml.Serialization.TypeData..ctor (System.Type type, System.String elementName, System.Boolean isPrimitive, System.Xml.Serialization.TypeData mappedType, System.Xml.Schema.XmlSchemaPatternFacet facet) [0x000dd] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs:115
at System.Xml.Serialization.TypeData..ctor (System.Type type, System.String elementName, System.Boolean isPrimitive) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs:86
at System.Xml.Serialization.TypeTranslator.GetTypeData (System.Type runtimeType, System.String xmlDataType, System.Boolean underlyingEnumType) [0x00294] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs:208
at System.Xml.Serialization.TypeTranslator.GetTypeData (System.Type type) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs:130
at System.Xml.Serialization.XmlReflectionImporter.GetReflectionMembers (System.Type type) [0x00258] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:771
at System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Boolean isBaseType) [0x00078] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:346
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00066] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:215

* 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) [0x0011d] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:235
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00041] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs:190
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) [0x00041] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:239
at System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Xml.Serialization.XmlRootAttribute root) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/

@nirinchev
Copy link
Member

Sounds like you can't use XmlSerializer as that doesn't allow you to ignore all properties by default (i.e. use an opt-in behavior as opposed to opt-out). You can instead use the DataContractSerializer and decorate your models with DataContract and DataMember attributes.

@fealebenpae
Copy link
Member

The problem is that the XML serializer tries to serialize the properties on the base RealmObject class, such as Realm and ObjectSchema.

@Doxoh according to this StackOverflow answer it should be possible to configure the XML serializer to ignore some properties. Perhaps you could configure it to ignore the properties on RealmObject so that only the properties in your derived model class are considered by the serializer? Another option should be DataContractSerializer like @nirinchev suggests.

@Doxoh
Copy link
Author

Doxoh commented Oct 19, 2019

@nirinchev @fealebenpae
If I use the DataContractSerializer I get the following error:

Type 'BAAS.Model.Telegram' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'Realms.RealmObject' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.

Did you have for this an idea? I didn't find an option for this.

@RealmBot RealmBot reopened this Dec 21, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants