Demo Effect:
usingNewtonsoft.json;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Runtime.Serialization.Json;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Xml;namespacejson goto Object { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { varvarentity = Toobject (@"<Response> <ResultCode>1</ResultCode> <resultmsg> ;</resultmsg> <Item> <DeptID>2001</DeptID> <DeptName> in-patient clinic </DeptName> </Item> <item& Gt <DeptID>2021</DeptID> <DeptName> out-of-chest clinic </DeptName> < /item> </Response>",false); List<testEntity> deptlist =NewList<testentity>(); DataTable DT=NewDataTable (); Dt. Columns.Add ("DeptID"); Dt. Columns.Add ("Deptname"); varObjectList =Varentity.item; foreach(varEntityiteminchobjectList) {testentity deptentity=Newtestentity (); Deptentity.deptid=Entityitem.deptid; Deptentity.deptname=Entityitem.deptname; Deptlist.add (deptentity); DataRow Dr=dt. NewRow (); dr["DeptID"] =Entityitem.deptid; dr["Deptname"] =Entityitem.deptname; Dt. Rows.Add (DR); } Datagridview1.datasource=deptlist; Datagridview2.datasource=DT; } /// <summary> ///XML parameter converted to object/// </summary> /// <param name= "Isaddhead" >whether to add an XML header</param> /// <returns></returns> Public Static DynamicToobject (stringXmlBOOLisaddhead) { stringHead =@"<?xml version= "" 1.0 "" encoding= "" Utf-8 ""?>"; XmlDocument Doc=NewXmlDocument (); if(Isaddhead) {doc. LOADXML (Head+XML); } Else{doc. LOADXML (XML); } stringJson =Jsonconvert.serializexmlnode (DOC); Dynamicobj = (Jsonconvert.deserializeobject (Json) as Dynamic)["Response"]; returnobj; } } Public classtestentity { Public stringDeptID {Get;Set; } Public stringDeptname {Get;Set; } }}
The XML parameter is converted to object and converted to a list or DataTable