xmlserializer - How to correctly (de)serialize nested objects in c#? -


edit: sorry code working intended. failed test properly. sorry inconvenience

i found code here on store , load objects (code in end). storing file correctly working, getting file object again not working when have list of objects:

executing

block b = loadfile<block>("file"); console.writeline(b.allcoins.count); //is 0 

results in empty list. checking xml file correctly stored, means loading somehow not working. how can correctly load object?

here block class:

[serializable]     public class block {         public struct coin {             public string owner;             public string name;              public coin(string n, string o) {                 owner = o;                 name = n;             }         };          public int name;         public list<string> hashofparticles;         public int numberoftransactions;         public list<coin> allcoins;      }  } 

here how load file objects:

public static t loadfile<t>(string filename) {             if (string.isnullorempty(filename)) { return default(t); }              t objectout = default(t);              try {                 xmldocument xmldocument = new xmldocument();                 xmldocument.load(filename);                 string xmlstring = xmldocument.outerxml;                  using (stringreader read = new stringreader(xmlstring)) {                     type outtype = typeof(t);                      xmlserializer serializer = new xmlserializer(outtype);                     using (xmlreader reader = new xmltextreader(read)) {                         objectout = (t)serializer.deserialize(reader);                         reader.close();                     }                      read.close();                 }             } catch (exception ex) {                 //log exception here             }              return objectout;         } 

here code stores file:

public static void storefile<t>(t serializableobject, string filename) {             if (serializableobject == null) { return; }              try {                 xmldocument xmldocument = new xmldocument();                 xmlserializer serializer = new xmlserializer(serializableobject.gettype());                 using (memorystream stream = new memorystream()) {                     serializer.serialize(stream, serializableobject);                     stream.position = 0;                     xmldocument.load(stream);                     xmldocument.save(filename);                     stream.close();                     form1.instance.addtolog("storing \"" + filename +  "\" succesful");                 }             } catch (exception ex) {                 console.writeline(ex.tostring());                 form1.instance.addtolog("storing \"" + filename + "\" not succesful");              }         } 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -