c# - xml de-serialization with a very simple file -


i started xml serialization , tried these:

(1) throws exception while deserializing. (2) throws exception (3) not working should be.

any ideas appreciated. in advance.

  1) const string xml = @"<?xml version=""1.0""?>                     <dietplan>                             <fruit>fig</fruit>                             <veggie>carrot</veggie>                     </dietplan>";      [xmlroot(elementname = "dietplan")]     public class testdata     {         [xmlelement("fruit")]         public xmlelement fruits { get; set; }          [xmlelement("veggie")]         public xmlelement test { get; set; }        }     2) const string xml = @"<?xml version=""1.0""?>                     <dietplan>                             <fruit>fig</fruit>                             <fruit>fig</fruit>                             <veggie>carrot</veggie>                             <veggie>carrot</veggie>                     </dietplan>";      [xmlroot(elementname = "dietplan")]     public class testdata     {         [xmlelement("fruit")]         public list<xmlelement> fruits { get; set; }          [xmlelement("veggie")]         public list<xmlelement> test { get; set; }        }   3) const string xml = @"<?xml version=""1.0""?>                     <dietplan>                         <data>                             <fruit>fig</fruit>                             <fruit>fig</fruit>                             <veggie>carrot</veggie>                             <veggie>carrot</veggie>                         </data>                     </dietplan>";      [xmlroot(elementname = "dietplan")]     public class testdata     {         public datas datas { get; set; }      }  public class datas {     [xmlelement("fruit")]     public list<xmlelement> fruits { get; set; }      [xmlelement("veggie")]     public list<xmlelement> test { get; set; }   } 

1) xml files valid in context! (though not on correct format)

2) code use de-serialize!

public static void deserialize()     {         var ms = new memorystream(encoding.utf8.getbytes(xml));         var xs = new xmlserializer(typeof(testdata));         var obj = (testdata)xs.deserialize(ms);      } 

edit: (3) has answer here enter link description here

if not need class members of type xmlelement, use string instead:

[xmlroot(elementname = "dietplan")] public class testdata {     [xmlelement("fruit")]     public string fruits { get; set; }      [xmlelement("veggie")]     public string test { get; set; }  } 

Comments

Popular posts from this blog

android - Inheriting from Theme.AppCompat* -

broadcastreceiver - android BOOT_COMPLETED not received if not activity intent-filter -

basic authentication with http post params android -