// <Summary> // serialize the object class to XML /// </Summary> /// <Param name = "enitities"> entity. </param> /// <Param name = "headtag"> node name </param> /// <returns> </returns> Public static string objlisttoxml <t> (list <t> enitities, string headtag) {stringbuilder sb = new stringbuilder (); system. reflection. propertyinfo [] propinfos = NULL; sb. appendline ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "); Sb. appendline ("<" + headtag + ">"); foreach (t obj in enitities) {// initialize propertyinfo if (propinfos = NULL) {type objtype = obj. getType (); propinfos = objtype. getproperties ();} sb. appendline ("<item>"); foreach (system. reflection. propertyinfo propinfo in propinfos) {sb. append ("<"); sb. append (propinfo. name); sb. append (">"); sb. append (propinfo. getvalue (OBJ, null); sb. append ("</"); sb. append (PR Opinfo. name); sb. appendline (">");} sb. appendline ("</item>");} sb. appendline ("</" + headtag + ">"); return sb. tostring ();} /// <summary> // convert the XML file to the object class list /// </Summary> /// <typeparam name = "T"> Object Name </typeparam> /// <Param name = "XML"> your xml file </param> /// <Param name = "headtag"> xml header file </param> /// <returns> Object List </returns> Public static list <t> xmltoobjlist <t> (string XML, string headtag) where T: New () {List <t> List = new list <t> (); system. XML. xmldocument Doc = new system. XML. xmldocument (); system. reflection. propertyinfo [] propinfos = NULL; Doc. loadxml (XML); system. XML. xmlnodelist nodelist = Doc. getelementsbytagname (headtag); foreach (system. XML. xmlnode node in nodelist) {T entity = new T (); // initialize propertyinfo if (propinfos = NULL) {type objtype = entity. getType (); propinfos = objtype. getpr Operties ();} // fill in the attributes of the entity class foreach (system. reflection. propertyinfo propinfo in propinfos) {// The first letter of the object type field is changed to a lower-case string name = propinfo. name. substring (0, 1) + propinfo. name. substring (1, propinfo. name. length-1); system. XML. xmlnode cNode = node. selectsinglenode (name); string v = cNode. innertext; If (V! = NULL) propinfo. setvalue (entity, convert. changetype (v, propinfo. propertytype), null) ;}list. Add (entity);} return list ;}
Convert object to XML to object