C # conversion between xmlstring and object class objects in development

Source: Internet
Author: User

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 ();}

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.