. Net parses XML strings and assigns values to object class objects through reflection to obtain the object class data list.

Source: Internet
Author: User
///   <Summary>          ///  Parses XML strings and converts them to Object List Data  ///   </Summary>          ///   <Param name = "xmlstr">  XML string  </Param>          ///   <Returns> </returns>          ///  The XML string format is as follows: (the node customers of item have multiple pairs, each representing a piece of data)  ///  <? XML version = "1.0" encoding = "UTF-8"?> <Boss> <item>          ///   <Userid>  User ID  </Userid>          ///   <Type>  User Type  </Type>          ///   <Group>  User Group  </Group>          ///   <State> User status  </State>          ///   <SIM>  SIM card number  </Sim>          ///   <STB>  Set-top box No.  </STB>          ///   <Memo>  Remarks  </Memo>          ///   <Crtime>  Creation date </Crtime>          ///   <Customerid>  Customer ID  </Customerid>          ///   <Customername>  CUSTOMER NAME  </Customername>          ///   <Address>  User address  </Address>          ///   <Phone>  Phone number </Phone>          ///   <Mobile>  Mobile phone  </Mobile>          ///   </Item> <code> </code> <MSG> </boss>          Public List <userinfo> getuserlist ( String  Xmlstr) {system. type T = Typeof  (Userinfo );  Object OBJ = activator. createinstance (t,Null ); //  Create an instance of the specified type Propertyinfo [] fields = obj. GetType (). getproperties (); //  Obtains all public attributes of a specified object.  List <Userinfo> ulist = New List <userinfo> (); Xmldocument XML = New  Xmldocument (); XML. loadxml (xmlstr );  //  Load XML Xmlnodelist xlist = xml. getelementsbytagname ( "  Item  " ); //  Get the item node list              Foreach (Xmlnode XN In Xlist) //  Traverse subnodes under the item Node  {  Object U = activator. createinstance (t, Null ); // Create an instance of the specified type                  Foreach (Propertyinfo P In  Fields ){  For ( Int I = 0 ; I < XN. childnodes. Count ;){  If (Xn. childnodes [I]. Name = P. Name) {P. setvalue (u, xn. childnodes [I]. innertext,  Null );//  Assign a value to the attributes of the created instance.  } I ++ ;}} Ulist. Add (userinfo) U );}  Return  Ulist ;} 

 

Or write this method as a general method for calling.

 

 ///   <Summary>          ///  Obtains the object data list of an object class.  ///  </Summary>          ///   <Param name = "xmlstr">  XML string in the specified format  </Param>          ///   <Param name = "T">  Object Class  </Param>          ///   <Returns>  Object Data Category of Object Class  </Returns>          ///   <Autor> Shangpeng  </Autor>          ///   <Datecreated>  20130709  </Datecreated>          ///  You cannot directly convert a data category to a list of real data types of the object class. during conversion, You need to traverse objects that are not converted to the object type one by one, and then attach them to the list          Public List < Object > Getobjectlist ( String  Xmlstr, type T) {list < Object > Olist =New List < Object > ();  Object OBJ = activator. createinstance (t, Null ); //  Create an instance of the specified type Propertyinfo [] fields = obj. GetType (). getproperties (); //  Obtains all public attributes of a specified object.  Xmldocument XML = New  Xmldocument (); XML. loadxml (xmlstr ); //  Load XML Xmlnodelist xlist = xml. getelementsbytagname ( "  Item  " ); //  Get the item node list              Foreach (Xmlnode XN In Xlist) //  Traverse subnodes under the item Node  {  Object U = activator. createinstance (t, Null ); //  Create an instance of the specified type                  Foreach (Propertyinfo P In  Fields ){  For ( Int I = 0 ; I < XN. childnodes. Count ;){  If (Xn. childnodes [I]. Name = P. Name) {P. setvalue (u, xn. childnodes [I]. innertext, Null ); //  Assign a value to the attributes of the created instance.  } I ++ ;}} Olist. Add (U );}  Return  Olist ;} 

 

String xmlstr = "XML string in the specified format ";

System. type T = typeof (entity class );

List <Object> olist = getobjectlist (xmlstr, t );

List <entity class> List = new list <entity class>

However, a problem exists here, that is, only the data list of the object class is obtained, and the list <Object> cannot be converted to the list <object class>, in this case, we can convert the data through traversal.

Foreach (Object o in olist)

{

List. Add (entity class) O );

}

 

 

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.