最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換

 更新時(shí)間:2015年11月20日 09:41:06   投稿:hebedich  
這篇文章主要介紹了C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換的相關(guān)代碼及示例,需要的朋友可以參考下

官方 JSON.NET 地址
http://james.newtonking.com/pages/json-net.aspx

XML TO JSON

string xml = @"<?xml version=""1.0"" standalone=""no""?>
<root>
 <person id=""1"">
 <name>Alan</name>
 <url>http://www.google.com</url>
 </person>
 <person id=""2"">
 <name>Louis</name>
 <url>http://www.yahoo.com</url>
 </person>
</root>";
 
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
 
string jsonText = JsonConvert.SerializeXmlNode(doc);
//{
// "?xml": {
//  "@version": "1.0",
//  "@standalone": "no"
// },
// "root": {
//  "person": [
//   {
//    "@id": "1",
//    "name": "Alan",
//    "url": "http://www.google.com"
//   },
//   {
//    "@id": "2",
//    "name": "Louis",
//    "url": "http://www.yahoo.com"
//   }
//  ]
// }
//}

JSON TO XML

string json = @"{
 ""?xml"": {
  ""@version"": ""1.0"",
  ""@standalone"": ""no""
 },
 ""root"": {
  ""person"": [
   {
    ""@id"": ""1"",
    ""name"": ""Alan"",
    ""url"": ""http://www.google.com""
   },
   {
    ""@id"": ""2"",
    ""name"": ""Louis"",
    ""url"": ""http://www.yahoo.com""
   }
  ]
 }
}";
 
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
// <?xml version="1.0" standalone="no"?>
// <root>
//  <person id="1">
//  <name>Alan</name>
//  <url>http://www.google.com</url>
//  </person>
//  <person id="2">
//  <name>Louis</name>
//  <url>http://www.yahoo.com</url>
//  </person>
// </root>

DEMO:JSON TO XML

string json_str = "{\"a\":\"a\",\"b\":\"b\"}";
//json 的字符串需要按照這個(gè)格式 書寫,否則會(huì)報(bào)錯(cuò)
string json = @"{
 ""?xml"": {
  ""@version"": ""1.0"",
  ""@standalone"": ""no""
 },
 ""root"":" + json_str + "}";
 
if (!string.IsNullOrEmpty(json))
{
  XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
   
}

相關(guān)文章

最新評(píng)論

巴塘县| 荔波县| 赤峰市| 桐乡市| 安乡县| 新民市| 凤山县| 潞城市| 南宁市| 两当县| 安徽省| 舞钢市| 吉隆县| 太白县| 泽普县| 盐津县| 盈江县| 广东省| 横山县| 贵阳市| 通城县| 平阳县| 盘锦市| 新化县| 巴东县| 灵宝市| 彭山县| 平江县| 北海市| 宝山区| 富裕县| 岢岚县| 城固县| 闵行区| 扎赉特旗| 葫芦岛市| 甘谷县| 德阳市| 东乡族自治县| 五莲县| 北票市|