直接上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)
{
//讀取檔案並儲存成string陣列
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\olympics-EventsData.tsv");
string[] jsonArray = null;
XmlDocument nodeKey = null;
XmlDocument content = null;
XmlElement elem = null;
//迴圈執行每一行
foreach (string line in lines)
{
//將每行的兩個json使用tab分開
jsonArray = line.Split(‘\t‘);
//先將第一個json轉換為xml並指定根結點為root
//input:
// { "NodeKey":"4e6d747a84e8531da692fb7f3d76c55c",
// "Market":"en-us"
// }
//ex:
// <root>
// <NodeKey>4e6d747a84e8531da692fb7f3d76c55c</NodeKey>
// <Market>en-us</Market>
// </root>
nodeKey = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonArray[0], "root");
//先將第一個json轉換為xml並指定根結點為nodeKey
//input:
// { "Events":[
// { "Name":"Opening Ceremony","EventId":"9dd7bc65f9b1553cbaaf5babef78dc25","Description":"","StartDate":"2016-08-05T23:00: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 ? 2016","Url":""}],
// "Node":{"DisplayName":"Ceremonies","Key":"4e6d747a84e8531da692fb7f3d76c55c","IsLeaf":true,"ThumbnailId":"https://www.bing.com/th?id=OSC.TOOLBC7157951DCC880CD23E99552CEB183379EF3D22CA0D9572709B07C93587AA6B","LastModifiedTime":"2016-09-21T10:05:39.1808394Z"},
// "ParentKey":"c6c6c971d0555d04bc8ddde678903ce1"
// }
//ex:
// <nodeKey>
// <Events>
// <Name>Opening 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</EventId><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="true" xmlns:json="http://james.newtonking.com/projects/json">
// <ProviderDisplayName>PERFORM LLC ? 2016</ProviderDisplayName><Url></Url>
// </Attributions>
// <Node>
// <DisplayName>Ceremonies</DisplayName><Key>4e6d747a84e8531da692fb7f3d76c55c</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);
//宣告一個變數,用來存放第二個xml的節點,並將節點的tag命名為Key
elem = content.CreateElement("Key");
//取得第一個xml的NodeKey值放進變數
elem.InnerText = nodeKey.FirstChild.FirstChild.InnerText;
//取得第二個xml的root節點,因為我們接下來要將先前宣告的變數放到第二個xml的root節點之下
XmlNode root = content.DocumentElement;
//將先前宣告的節點變數(elem)塞到第二個xml root節點下的第一個元素(root.FirstChild)之前(InsertBefore)
root.InsertBefore(elem, root.FirstChild);
//將轉換且組合的結果打印出,如果需要存回檔案可替換此部分程式碼
Console.WriteLine(content.InnerXml);
}
//程式結束
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}