One, the XML format is converted into JSON data format
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;//to use JavaScriptSerializer, you need to add this using System.Web.Script.Serialization; referenceusingSystem.Web.Script.Serialization;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;//to use XDocument, you need to add this using SYSTEM.XML.LINQ referenceusingSystem.Xml.Linq;//to use JsonConvert you need to add this Newtonsoft.Json.dll referenceusingNewtonsoft.json;usingSystem.Xml; Public Partial classxmlchange:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { varXML =@"<Columns> <column name= "" Key1 "" >key1</Column> <column name= "" Key2 "" >key2< /column> <column name= "" Key3 "" >key3</Column> </Columns>"; varDIC =XDocument. Parse (XML). Descendants ("Column") . ToDictionary (c= C.attribute ("Name"). Value, C=c.value); varJSON =NewJavaScriptSerializer (). Serialize (DIC); Label1.Text=JSON; XML Model= jsonconvert.deserializeobject<xml>(JSON); Label2.Text=Model.key1; Label3.text=Model.key2; Label4.text=Model.key3; } Public classXML { Public stringKey1 {Get;Set; } Public stringKey2 {Get;Set; } Public stringKey3 {Get;Set; } }}
Second, serialization and deserialization of JSON data format
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Script.Serialization;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classjsonchange:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Test Te=Newtest (); Te.test1=1; Te.test2="Test"; JavaScriptSerializer Jsonserializer=NewJavaScriptSerializer (); //perform serialization stringR1 =jsonserializer.serialize (TE); Label1.Text=R1; //Performing deserializationTest per = jsonserializer.deserialize<test>(R1); Label2.Text=per.test1.ToString (); Label3.text=Per.test2; } Public classTest { Public inttest1 {Get;Set; } Public stringtest2 {Get;Set; } }}
Simple processing of XML and JSON data