c# - Derived Class Deserialization -


There is a problem with deserialization with my logic simulation program. These are my element classes: < Pre> public class AndGateData: two inputgate data {} public class two inputgate data: gate data {public two inputgate data () {Input2 = new InputData (); Input 1 = new input data (); } Public InputData Input1 {get; Set; } Public Input Data Input2 {get; Set; }} Public class gatetta: elementadata {public gate data () {outputdata = new output data (); } {Receive public output data output data; Set; }} Public Class Elementadata {public int delay {get; Set; } Get Public Guide ID { Set; }}

And here are the sections responsible for sections:

  Public category inputdata: socketdata {} public class socketdata {public guide id {get; Set; } Receive Public Signal Data Signals { Set; }}   

Signal data is not important here. So, I will not write it (to keep this question clear) Even until someone says that it is not necessary.

CircuitData is very important:

  [XmlRoot ("circuits")] public class circuits [XmlElement (typeg (orgatedata)) [XmlElement (typef (orgatedata)) ] Public List & LT; Elementandata & gt; Element = new list & lt; ElementData & gt; (); Public listing & lt; WireData & gt; Wire = new list & lt; WireData & gt; (); Public Zero Adelament (Elementadata Element) {elements.Add (element); } Public Zero AddWire (WireData Wire) {Wired. Add (wire); }}   

The strings are not important anymore. Now, I have written some of the serialization:

  public class circuits detergent {write public static void (object obj , String filename) {var xmlFormat = new XmlSerializer (Typef (CircuitData)); (Stream fstream = new filestream (using filename, file mode.creative, fileexe.Weight, fileshore.NON)) {xmlFormat.Serialize (fStream, obj); } Console. WrightLine ("Saved in Circuit XML Format."); }}   

This works as I wanted, it produces the XML document:

   & Lt; / Input 1 & gt; - & lt; Input 2 & gt; & Lt; Eid & gt; D2cad8f8-8528-4db3-9534-9baadb6a2a14 & lt; / Id & gt; & Lt; Signal data xsi: type = "signaldataoff" /> & Lt; / Input 2 & gt; & Lt; / AndGateData> & Lt; Stars / & gt; & Lt; / Circuits & gt;   

But I have a problem with my classification. Here's the code:

  Read public static circuits () {var reader = New XmlSerializer (Typef (CircuitData)); Streamer file = new streamer ("circuit.exml"); Var returnCircuitData = new circuit data (); RefundCard data = (CircuitData) Reader. Diaryialize (file); Return returnscrudeddata; }   

Now, it deserializes my Circuit.xml for this object, but in this object, only Id and Delay , does not contain Input1 , Input2 or output . Therefore, it is treated like the element , such as AndGate not. I tried to solve it for one day but it seems that there is no such problem.

I have a suggestion for you, type Create and objectToSerialize.GetType () :

  Create serializer by using the public Write zero zero & lt; T & gt; (T ObjectusTorialize, string filename) {var xmlSerializer = New XML SerialIizer (objectToSerialize.GetType ()); ...}   

Gives the object to this method, you can make your read the common method like this:

  Public stable T & Lt; T & gt; (String filename) {var serializer = new XML serializer (typef (t)); (Using the streamrider file = new streamrider (filename)) {Return (T) Serializer. Diaryialize (file); Besides, you may want to read about it:  
  • It is used when you can serialize derived sections.
  • and is used to control the ordering of arrays

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -