C# json转xml
标签:write geo events use form rtb creat 命名 des
直接上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:
//
// 4e6d747a84e8531da692fb7f3d76c55c
// en-us
//
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:
//
//
// Opening Ceremony9dd7bc65f9b1553cbaaf5babef78dc252016-08-05T23:00:00Z2016-08-06T04:00:00ZMaracan? Stadium00Rio de JaneiroBrazil2016-09-21T10:05:39.1808394Zfalse
//
//
// Closing Ceremony72fd07eca2ce5781bee6977e3ca84c4e2016-08-21T23:16:00Z2016-08-22T03:16:00ZMaracan? Stadium00Rio de JaneiroBrazil2016-09-21T10:05:39.1818375Zfalse
//
//
// PERFORM LLC ? 2016
//
//
// Ceremonies4e6d747a84e8531da692fb7f3d76c55ctruehttps://www.bing.com/th?id=OSC.TOOLBC7157951DCC880CD23E99552CEB183379EF3D22CA0D9572709B07C93587AA6B2016-09-21T10:05:39.1808394Z
//
// c6c6c971d0555d04bc8ddde678903ce1
//
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();
}
}
}
C# json转xml
标签:write geo events use form rtb creat 命名 des
原文地址:https://www.cnblogs.com/xxh-2014/p/8427350.html
评论