[英]Serialize error for a class not present in class being serialized
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. Stations
和Systems
类都没有任何JObjects
,但错误提示我需要默认访问器。 I do use JObjects
but not in those classes or even in the settings
class except a JSON deserializer.我确实使用JObjects
,但没有在这些类中使用,甚至没有在settings
class 中使用,但 JSON 反序列化器除外。 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这是两个类和子类: pastebin.com
Your model contains:您的 model 包含:
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.由于这些是数据中唯一不明显的类型,我猜测其中一个(或两个)是罪魁祸首,包含一些JObject
数据。 Presumably: replace them with things that... don't .大概是:用……不要的东西代替它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.