usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml.Linq;namespacexml{classProgram {Static voidMain (string[] args) { //Create a generic collectionlist<students> list =NewList<students>(); //reading DataDataTable TB = Mysqlhelper.executequery ("SELECT * from T_student"); if(TB. Rows.Count >0) { //have data foreach(DataRow RinchTB. Rows) {Students Stu=rowtostudent (R); if(Stu! =NULL) {list. ADD (Stu); }}} XDocument Xdoc=NewXDocument (); //XML root NodeXElement root =NewXElement (" Person"); //iterating through the collection for(inti =0; I < list. Count; i++) {Students Stu=List[i]; XElement Student=NewXElement ("Student"); Student. Setattributevalue ("Stuid", Stu.id. ToString ()); Student. Setelementvalue ("Stuname", Stu. Name); Student. Setelementvalue ("Stuage", Stu. Age); Student. Setelementvalue ("Stugender", Stu. Gender); Root. ADD (student); } xdoc. ADD (root); //SaveXdoc. Save (@"C:\Users\Administrator\Desktop\xml\stu.xml"); Console.WriteLine ("all data Export!"); Console.readkey (); } Public StaticStudents rowtostudent (DataRow DR) {//assigning values to students object fieldsStudents Stu =Newstudents (); Stu.id= Convert.ToInt32 (dr["ID"]); Stu. Name= dr["name"]. ToString (); Stu. Age= Convert.ToInt32 (dr[" Age"]); Stu. Gender= dr["Gender"]. ToString (); returnStu; } }}
C # database Data export XML