c# - How to debug XML deserialization? -


i wondering if had tips on how can debug below xml deserialization? cannot work. deserializer creates summon , slash instances, properties empty. relevant classes shown below.


skillcollection class deserializer:

[datacontract(name = "skills", namespace = "")] public class skillcollection {  [datamember(name = "slash")] public skill slash { get; set; }  [datamember(name = "summon")] public skill summon { get; set; }  public static object deser(string path, type totype) {     var s = new datacontractserializer(totype);     using (filestream fs = file.open(path, filemode.open))     {         object s2 = s.readobject(fs);         if (s2 == null)             console.writeline(@"  deserialized object null");         else             console.writeline(@"  deserialized type: {0}", s2.gettype());         return s2;     } } 

it called class through property skills:

skills = (skillcollection)skillcollection.deser(             path.combine(path, "skills.xml"),              typeof(skillcollection)); 

skill class:

public class skill {     //cast: time takes cast     [datamember(name = "cast")]     public float cast { get; set; }      //recast: cooldown period before player can cast again     [datamember(name = "recast")]     public float recast { get; set; }      [datamember(name = "mpcost")]     public int mpcost { get; set; }      public timer timer { get; private set; }     public bool ready { get; set; }      public skill()     {         ready = true;          timer = new timer { interval = recast + 500, autoreset = false };         timer.elapsed += ontimedevent;     }      //runs when recast     private void ontimedevent(object source, elapsedeventargs e)     {         ready = true;     } } 

xml file:

<skills>   <slash>     <cast>0.00</cast>     <recast>60.00</recast>     <mpcost>0</mpcost>   </slash>   <summon>     <cast>5.98</cast>     <recast>2.49</recast>     <mpcost>0</mpcost>   </summon> </skills> 

just there no confusion, goal run deserializer, , have skillcollection class contain 2 instances of skill (slash , summon), , able access them separately through properties.

thanks / tips debugging this.

anders,

i did similar task. looking @ code seems set datacontract name , namespace wrong. both above skill class , skillcollection class need specify: [datacontract(name = "skills", namespace = "")]. critical serializer "allowed" enter properties. keep in mind setting above skillcollection class not enough.

edit: thought leave out namespace, cannot. has set either namespace="", in case not specify namespace in xml, or whatever specify namespace be, in case specify it.

also, way debug change deser method this:

    public static object deserialize(string path, type totype)     {         using (var sr = new filestream(path, filemode.open))         {             skillcollection p = null;             xmldictionaryreader reader = xmldictionaryreader.createtextreader(sr, new xmldictionaryreaderquotas());             var serializer = new datacontractserializer(totype);              while (reader.read())             {                 switch (reader.nodetype)                 {                     case xmlnodetype.element:                         if (serializer.isstartobject(reader))                         {                             console.writeline(@"found element");                             p = (skillcollection)serializer.readobject(reader);                             console.writeline("{0} {1}    id:{2}",                                 p.slash.tostring(), p.summon.tostring());                         }                         console.writeline(reader.name);                         break;                 }             }              return p;         }     } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -