[C#] XML format convert to JSON

2012-12-15

 

現在JSON 已經變成係在資料交換的當紅炸子雞,因為速度跟方便,雖然他在資料敘述上面,並非XML來的工整..

最近遇到跟一些比較'資深' 的廠商合作,它們提供XML

想到要自己處理DOM 有點麻煩,這時候研究一下怎麼轉JSON.

先看一下XML 範本 sample.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
  <Row>
        <use>0</use>
        <page>1</page>
        <pic_file_name>a.jpg</pic_file_name>
        <product_name>A商品</product_name>
        <url>http://www.msdn.com</url>
    </Row>
    <Row>
        <use>1</use>
        <page>1</page>
        <pic_file_name>b.jpg</pic_file_name>
        <product_name>B商品</product_name>
        <url>http://www.msdn.com</url>
    </Row>
    <Row>
        <use>2</use>
        <page>1</page>
        <pic_file_name>c.jpg</pic_file_name>
        <product_name>C商品</product_name>
        <url>http://www.msdn.com</url>
    </Row>
    <Row>
        <use>3</use>
        <page>2</page>
        <pic_file_name>d.jpg</pic_file_name>
        <product_name>D商品</product_name>
        <url>http://www.msdn.com</url>
    </Row>
    <Row>
        <use>4</use>
        <page>2</page>
        <pic_file_name>e.jpg</pic_file_name>
        <product_name>E商品</product_name>
        <url>http://www.msdn.com</url>
    </Row>
    
</Root>


很簡單易懂,五筆資料,常見的XML 檔案..


我們要怎麼轉成 XML 呢…


最快的方法 


首先先安裝 Newtonsoft.Json





Install-Package Newtonsoft.Json



使用NUGET 安裝,快速又簡單…這時候只需要透過


XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "sample.xml"));
string json = JsonConvert.SerializeXmlNode(xDoc);
Response.Write(json);


只需要使用XMLDocument 將XML 載入後,將他丟給JsonConvert 就可以序列化成 JSON
等等…
出來的結果..





{"?xml":{"@version":"1.0","@encoding":"UTF-8","@standalone":"yes"},"Root":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","Row":[
{"use":"0","page":"1","pic_file_name":"a.jpg","product_name":"A商品","url":"http://www.msdn.com"},

{"use":"1","page":"1","pic_file_name":"b.jpg","product_name":"B商品","url":"http://www.msdn.com"},

{"use":"2","page":"1","pic_file_name":"c.jpg","product_name":"C商品","url":"http://www.msdn.com"},

{"use":"3","page":"2","pic_file_name":"d.jpg","product_name":"D商品","url":"http://www.msdn.com"},

{"use":"4","page":"2","pic_file_name":"e.jpg","product_name":"E商品","url":"http://www.msdn.com"}]}}




第一行表頭也被序列化成JSON了,這不是我想要的..

這時候必須要多加一行…

xDoc.RemoveChild(xDoc.FirstChild);

把第一個XML文件宣告部份給拿掉..


所以最後的Code :


XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "sample.xml"));
xDoc.RemoveChild(xDoc.FirstChild);
string json = JsonConvert.SerializeXmlNode(xDoc);
 
Response.Write(json);


這樣最後結果出來:


{"Root":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","Row":[
{"use":"0","page":"1","pic_file_name":"a.jpg","product_name":"A商品","url":"http://www.msdn.com"},
{"use":"1","page":"1","pic_file_name":"b.jpg","product_name":"B商品","url":"http://www.msdn.com"},
{"use":"2","page":"1","pic_file_name":"c.jpg","product_name":"C商品","url":"http://www.msdn.com"},
{"use":"3","page":"2","pic_file_name":"d.jpg","product_name":"D商品","url":"http://www.msdn.com"},
{"use":"4","page":"2","pic_file_name":"e.jpg","product_name":"E商品","url":"http://www.msdn.com"}]}} 


就好看多了.. 








當麻許的超技八 2014 | Donma Hsu Design.