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

11
  • Are you sure? What are settings.Stations and settings.Systems? Post the code Form1_FormClosing and the definitions of those objects Commented Sep 22, 2020 at 10:54
  • @PanagiotisKanavos The classes are 200+ lines of code so I made it into a Pastebin. Commented Sep 22, 2020 at 11:01
  • Nobody is going to read it there. Post the relevant parts. Somehow, somewhere, a JObject is used Commented Sep 22, 2020 at 11:02
  • More importantly, post the code that throws - the Form1_Closing event handler, specifically line 322. Are you serializing the wrong object perhaps? Commented Sep 22, 2020 at 11:04
  • 2
    You have object in Minor_Faction_Presences - any chance it is that? Commented Sep 22, 2020 at 11:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.