C# List<> to xml -


calling

list<pc> _pclist = new list<pc>(); ...add pc pclist..  writexml<list<pc>>(_pclist, "ss.xml"); 

function

public static void writexml<t>(t o, string filename) {      string filepath= environment.getfolderpath(environment.specialfolder.applicationdata) + "\\genweb2\\adsnopper\\" + filename;      xmldocument xmldoc = new xmldocument();     xpathnavigator nav = xmldoc.createnavigator();     using (xmlwriter writer = nav.appendchild())     {         xmlserializer ser = new xmlserializer(typeof(list<t>), new xmlrootattribute("therootelementname"));         ser.serialize(writer, o); // error     }     file.writealltext(filepath,xmldoc.innerxml);  } 

inner exception

unable cast object of type 'system.collections.generic.list1[pc]' type 'system.collections.generic.list1[system.collections.generic.list`1[pc]]'.

please help

the problem line

xmlserializer ser = new xmlserializer(typeof(list<t>), ... 

your t list<pc>, , you're trying create typeof(list<t>), translate typeof(list<list<pc>>). make typeof(t) instead.


Comments

Popular posts from this blog

PHP Error "Undefined index" -

php - How to only save new tweets/instagram photos to database? -