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