C # JSON Goto XML

Source: Internet
Author: User

Directly on code:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Xml;using Newtonsoft.json;namespace jsontoxml{class Program {static void Main (string[] args) {//Read Files are stored as String array string[] lines = System.IO.File.ReadAllLines (@ "C:\Users\Administrator\Desktop\olympics-EventsDat            A.TSV ");            string[] Jsonarray = null;            XmlDocument nodekey = null;            XmlDocument content = null;            XmlElement elem = null; Loop performs each line of foreach (string in lines) {//will use tab to separate two JSON per line Jsonarr ay = line.                Split (' \ t ');                First, transform the first JSON into XML and specify root//input://{"Nodekey": "4e6d747a84e8531da692fb7f3d76c55c",                "Market": "en-us"//}//EX://<root> <NodeKey>4e6d747a84e8531da692fb7f3d76c55c</NodeKey>                <Market>en-us</Market>//</root> Nodekey = (xmld                ocument) Jsonconvert.deserializexmlnode (jsonarray[0], "root"); First, transform the first JSON into XML and specify the root point as Nodekey//input://{"Events": [//{"Name": " Opening ceremony "," EventId ":" 9dd7bc65f9b1553cbaaf5babef78dc25 "," Description ":" "," StartDate ":" 2016-08-05t23:0 0:00.0000000z "," EndDate ":" 2016-08-06t04:00:00.0000000z "," location ": {" Name ":" Maracan? " Stadium "," Geo ": {" Latitude ": 0.0," Longitude ": 0.0}," Address ": {" addresslocality ":" "," addressregion ":" Rio de Janeiro " , "PostalCode": "", "Addresscountry": "Brazil"}, "Lastmodifiedtime": "2016-09-21t10:05:39.1808394z", "tobeannounced" : false},//{"Name": "Closing Ceremony", "EventId": "72fd07eca2ce5781bee6977e3ca84c4e", "Description": "", "StartDate": "2016-08-21t23:16:00.0000000z", "EndDate": "2016-08-22t03:16:00.0000000z", "location": {"Name": " Maracan? Stadium "," Geo ": {" Latitude ": 0.0, "Longitude": 0.0}, "Address": {"addresslocality": "", "addressregion": "Rio de Janeiro", "PostalCode": "", "    Addresscountry ":" Brazil "}}," Lastmodifiedtime ":" 2016-09-21t10:05:39.1818375z "," tobeannounced ": false}],// "Attributions": [//{"Providerdisplayname": "PERFORM LLC? "," "Url": ""}],//"Node": {"DisplayName": "Ceremonies", "Key": "4e6d747a84e8531da692fb7f3d76c55c", "isleaf ": True," Thumbnailid ":" Https://www.bing.com/th?id=OSC. toolbc7157951dcc880cd23e99552ceb183379ef3d22ca0d9572709b07c93587aa6b "," Lastmodifiedtime ":" 2016-09-21T10:0 5:39.1808394z "},//" Parentkey ":" C6c6c971d0555d04bc8ddde678903ce1 "//}/ /ex://<nodeKey>//<Events>//<name>openi Ng ceremony</name><eventid>9dd7bc65f9b1553cbaaf5babef78dc25</eventid><description></ Description><startdate>2016-08-05t23:00:00z</startDate><enddate>2016-08-06t04:00:00z</enddate><location><name>maracan? Stadium</name><geo><latitude>0</latitude><longitude>0</longitude></geo ><address><addresslocality></addresslocality><addressregion>rio de Janeiro</ addressregion><postalcode></postalcode><addresscountry>brazil</addresscountry></ address></location><lastmodifiedtime>2016-09-21t10:05:39.1808394z</lastmodifiedtime><                tobeannounced>false</tobeannounced>//</Events>//<Events> <name>closing Ceremony</name><eventid>72fd07eca2ce5781bee6977e3ca84c4e</ev entid><description></description><startdate>2016-08-21t23:16:00z</startdate>< Enddate>2016-08-22t03:16:00z</enddate><location><name>maracan? stadium</name><geo><latitude>0</latitude><longitude>0</longitude></geo><address>< Addresslocality></addresslocality><addressregion>rio de janeiro</addressregion>< postalcode></postalcode><addresscountry>brazil</addresscountry></address></ Location><lastmodifiedtime>2016-09-21t10:05:39.1818375z</lastmodifiedtime><tobeannounced> false</tobeannounced>//</Events>//<attributions json:array= "Tru E "xmlns:json=" Http://james.newtonking.com/projects/json ">//<providerdisplayname>perfor M LLC?      2016</providerdisplayname><url></url>//</Attributions>// <Node>//<displayname>ceremonies</displayname><key>4e6d747a84e8531da692 Fb7f3d76c55c</key><isleaf>true</isleaf><thumbnailid>https://www.Bing.com/th?id=osc. Toolbc7157951dcc880cd23e99552ceb183379ef3d22ca0d9572709b07c93587aa6b</thumbnailid><lastmodifiedtime >2016-09-21T10:05:39.1808394Z</LastModifiedTime>//</Node>//<  parentkey>c6c6c971d0555d04bc8ddde678903ce1</parentkey>//</nodeKey> content                                = (XmlDocument) jsonconvert.deserializexmlnode (jsonarray[1], "Nodekey", true); Declare a variable, use it to store the second XML section, and name the dot tag key elem = content.                CreateElement ("Key"); Get the Nodekey value of the first XML and put the change elem.                InnerText = NodeKey.FirstChild.FirstChild.InnerText; Get the root of the second XML because we're going to put the previously declared variables below the root of the second XML XmlNode root = content.                DocumentElement; Plug the previously declared point Change (Elem) to the first element under the second XML root (root). FirstChild) before (insertbefore) root. InsertBefore (Elem, Root.                FirstChild);  Prints the result of the conversion and the combination, and if you need to save the file, you can replace this part of the code              Console.WriteLine (content.            INNERXML);            }//Program End Bundle Console.WriteLine ("Press any key to exit.");        System.Console.ReadKey (); }    }}

  

C # JSON Goto XML

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: info-contact@alibabacloud.com 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.