c# - Completely stuck at this XML Deserialization -


i cannot life of me figure out why unable deserialize xml class instances. please see 2 approaches have tried (and respective error messages), below.

method one:

    public static skillcollection deserialize(string path)     {         using (memorystream memorystream = new memorystream(encoding.utf8.getbytes(path.combine(path, "skills.xml"))))         {             skillcollection skills = null;             try             {                 var serializer = new datacontractserializer(typeof(skillcollection));                 var reader = xmldictionaryreader.createtextreader(memorystream, encoding.utf8, new xmldictionaryreaderquotas(), null);                 skills = (skillcollection)serializer.readobject(memorystream);             }             catch (serializationexception ex)             {                 globals.instance.applicationlogger.log("the object graph not deserialized binary stream because of following error: " + ex);             }             return skills;         }     } 

called this: skills = skillcollection.deserialize(path);

error: xmlexception thrown: unexpectedendoffile


method two:

    public static object deserialize(string xml, type totype)     {         console.writeline("file exists? " + file.exists(xml));         using (memorystream memorystream = new memorystream(encoding.utf8.getbytes(xml)))         {             xmldictionaryreader reader = xmldictionaryreader.createtextreader(memorystream, encoding.utf8, new xmldictionaryreaderquotas(), null);             datacontractserializer serializer = new datacontractserializer(totype);             return serializer.readobject(reader);         }     } 

called this: skills = (skillcollection) skillcollection.deserialize(path.combine(path, "skills.xml"), typeof(skillcollection));

error 1: xmlexception thrown: "the data @ root level invalid. line 1, position 1."

error 2: serializationexception thrown: "there error deserializing object of type magbot_ffxiv.skillcollection. data @ root level invalid. line 1, position 1."


my xml:

<skills>   <fire>     <cast>0.00</cast>     <recast>60.00</recast>     <mpcost>0</mpcost>     <button>0</button>   </fire>   <ice>     <cast>2.49</cast>     <recast>2.49</recast>     <mpcost>9</mpcost>     <button>1</button>   </ice> </skills> 

thanks million help.


update:

i used use linq2xml before, did not in case. please see post made earlier today: xml dictionary instances of custom class


my final code below. works perfectly, hope others out there.

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

notes:

  • keep in mind xml elements have in alpha order datacontractserializer work (that not apply xmlserializer)
  • also, must include [datacontract] attribute name , namespace above both skill , skillcollection classes, work

both of methods have fundamental, fixed bug.

in method 1, line:

new memorystream(encoding.utf8.getbytes(path.combine(path, "skills.xml"))) 

is wrong, because memory stream receives byte representation of path of file, instead actual contents of it.

in method2, guess same error, parameter name "xml" misleading. since you're first checking file existence xml, assume means path of file, , not contents.

try instead:

new memorystream(encoding.utf8.getbytes(file.readalltext(path.combine(path, "skills.xml")))) 

assuming actual file contents formatted correctly datacontractserializer should work. if doesn't, suggest first serialize live skillscollection object , see looks like, , use starting point.


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 -