Serialize error for a class not present in class being serialized

0

I'm trying to serialize data but a strange error occurs:

System.InvalidOperationException
  HResult=0x80131509
  Message=There was an error generating the XML document.
  Source=System.Xml
  StackTrace:
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o)
   at Starbreaker.Form1.Form1_FormClosing(Object sender, FormClosingEventArgs e) in C:\Users\Doven\source\repos\Starbreaker\Starbreaker\Form1.cs:line 322
   at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
   at System.Windows.Forms.Form.WmClose(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: You must implement a default accessor on Newtonsoft.Json.Linq.JObject because it inherits from ICollection.

The way I'm trying to serialize the data:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(setting));
    xmlSerializer.Serialize(new FileStream(settings.Xml, FileMode.Create), settings);
    xmlSerializer = new XmlSerializer(typeof(List<Station>));
    xmlSerializer.Serialize(new FileStream(settings.StationsXml, FileMode.Create), settings.Stations);
    xmlSerializer = new XmlSerializer(typeof(List<Systems>));
    xmlSerializer.Serialize(new FileStream(settings.SystemsXml, FileMode.Create), settings.Systems); // error thrown here
}

Both of the Stations and Systems classes don't have any JObjects yet the error cites that I need a default accessor. I do use JObjects but not in those classes or even in the settings class except a JSON deserializer. Another strange thing that happens is the file suddenly cuts off at the same place each time:

...
          <Active_States>
            <id>80</id>
            <name>None</name>
          </Active_States>
        </active_states>
        <pending_states />
        <recovering_states />
      </Minor_Faction_Presences>
      <Minor_Faction_Presences>
        <happiness_id>2</happiness_id>
        <minor_faction_id>42317</minor_faction_id>
        <influence>12.249</influence>
        <active_states>
          <Active_States>
            <id>73</id>
            <name>War</name>
          </Active_States>
        </active_states>
        <pending_states />
        <recovering_states />
      </Minor_Faction_Presences>
    </minor_faction_presences>
    <ed_system_address>2656194005347</ed_system_address>
  </Systems>
  <Systems>
    <id>4</id>
    <edsm_id>19210</edsm_i[eof]

The first three objects seem to sterilize fine but it fails at the beginning of the fourth. Here are the two classes and subclasses: pastebin.com

c#
serialization
asked on Stack Overflow Sep 22, 2020 by Doshorte Dovencio • edited Sep 22, 2020 by Doshorte Dovencio

1 Answer

1

Your model contains:

        public object[] pending_states { get; set; }
        public object[] recovering_states { get; set; }

Since those are the only non-obvious types in the data, I'm guessing that one (or both) of these is the culprit, containing some JObject data. Presumably: replace them with things that... don't.

answered on Stack Overflow Sep 22, 2020 by Marc Gravell

User contributions licensed under CC BY-SA 3.0