• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#json转xml

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

直接上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();
        }
    }
}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#获取和设置鼠标屏幕位置发布时间:2022-07-13
下一篇:
第二篇:C++画圆发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap