System.InvalidOperationException When Creating XmlSerializer

Nathan Sokalski 4,121 Reputation points
2021-01-23T19:18:18.05+00:00

I am attempting to create an XmlSerializer, and receive the following Exception:

System.InvalidOperationException: 'There was an error reflecting type 'PointTracker_Forms.TrackerName[]'.'

The line used to create the XmlSerializer is the following:

Dim xml As New XmlSerializer(GetType(T), New XmlRootAttribute(xmlroot))

And the TrackerName class is defined as follows:

using System.Diagnostics;
using System.Xml.Serialization;
using Xamarin.Forms;

namespace PointTracker_Forms
{
    [XmlRoot("TrackerName"), XmlType("TrackerName"), DebuggerDisplay(@"TrackerName   Name: {this.Name}   Id: {this.Id}")]
    public class TrackerName : BindableObject
    {
        public static readonly BindableProperty NameProperty = BindableProperty.Create("Name", typeof(string), typeof(TrackerName), "");
        public static readonly BindableProperty IdProperty = BindableProperty.Create("Id", typeof(int), typeof(TrackerName), 0);

        [XmlAttribute()] public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
        [XmlAttribute()] public int Id
        {
            get { return (int)GetValue(IdProperty); }
            set { SetValue(IdProperty, value); }
        }

        public TrackerName() : this("", 0) { }
        public TrackerName(string name, int id) { this.Name = name; this.Id = id; }

        public override string ToString() { return $"TrackerName   Name = \"{this.Name}\"   Id = {this.Id}"; }
    }
}

You will notice that the TrackerName class DOES have a default/parameterless constructor. The 2 properties (Name & Id) are just a string & int (they are not any fancy types). You may also notice that there are 2 BindableProperty(s). I tried adding the XmlIgnore() attribute to these, but it did not help (I actually tried removing them completely & using regularly properties, but that didn't make any difference either). Everything I could find just tells me to make sure I have a default/parameterless constructor, but like I said, I do. What is the problem? Thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,467 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,135 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2021-01-24T01:29:52.12+00:00

    If the BindableObject does not serialize to xml properly you could always try changing the base class for TrackerName . Maybe ObservableObject from MVVM Helpers will work for you.

    The other option is try and serialize as json instead of xml

    0 comments No comments