C#中使用JSON.NET实现JSON、XML相互转换

2019-12-10 19:52:22王冬梅

易采站长站为您分析C#中使用JSON.NET实现JSON、XML相互转换的相关代码及示例,需要的朋友可以参考下

官方 JSON.NET 地址

http://www.easck.com/pages/json-net.aspx

XML TO JSON

 

 
  1. string xml = @"<?xml version=""1.0"" standalone=""no""?>  <root> 
  2. <person id=""1"">  <name>Alan</name> 
  3. <url>http://www.easck.com/url>  </person> 
  4. <person id=""2"">  <name>Louis</name> 
  5. <url>http://www.easck.com/url>  </person> 
  6. </root>";   
  7. XmlDocument doc = new XmlDocument();  doc.LoadXml(xml); 
  8.   string jsonText = JsonConvert.SerializeXmlNode(doc); 
  9. //{  // "?xml": { 
  10. // "@version": "1.0",  // "@standalone": "no" 
  11. // },  // "root": { 
  12. // "person": [  // { 
  13. // "@id": "1",  // "name": "Alan", 
  14. // "url": "http://www.easck.com// }, 
  15. // {  // "@id": "2", 
  16. // "name": "Louis",  // "url": "http://www.easck.com// }  // ] 
  17. // }  //}