1. xmlstring is converted into an object class Object
xmlstring is converted into an object class object, that is, XML parsing and assigned a value to the object class.
for example, parse XML and generate a cost object class:
Public static object getcosts (string xmlstring) {model. costs mycosts = new costs (); // XML Parse xmldocument Doc = new xmldocument (); Doc. loadxml (xmlstring); xmlnodelist xxlist = Doc. getelementsbytagname ("object"); // obtain the set foreach (xmlnode xxnode in xxlist) of the node named object // xxnode is each
...
body {xmlnodelist childlist = xxnode. childnodes; // gets the subnode set foreach (xmlnode node in childlist) {string temp = node. name; Switch (temp) {Case "ID": // code mycosts. id = node. innertext; break; Case "item_code": // material code: mycosts. item_code = node. innertext; break ;}}return mycosts ;}
The XML style is as follows:
<Objects> <Object> <ID> 100 </ID> <item_code> 1 </item_code> </Object> </objects>
2. convert an object class object to an xmlstring
Public static string objlisttoxml <t> (list <t> enitities) {stringbuilder sb = new stringbuilder (); propertyinfo [] propinfos = NULL; // sb. appendline ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "); Sb. appendline ("<objects>"); foreach (t obj in enitities) {// initialize propertyinfo if (propinfos = NULL) {type objtype = obj. getType (); propinfos = objtype. getproperties ();} sb. appendline ("<Object>"); foreach (propertyinfo propinfo in propinfos) {sb. append ("<"); sb. append (propinfo. name); sb. append (">"); sb. append (propinfo. getvalue (OBJ, null); sb. append ("</"); sb. append (propinfo. name); sb. appendline (">");} sb. appendline ("</Object>");} sb. appendline ("<objects>"); return sb. tostring ();}