Simple processing of XML and JSON data

Source: Internet
Author: User

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{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{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

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: 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.