Convert object to XML to object

Source: Internet
Author: User
// <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.