C#JSON與XML相互轉換


 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Xml;

  6. using Newtonsoft.Json;

  7.  
  8. namespace JSonConverter

  9. {

  10. class Program

  11. {

  12. static void Main(string[] args)

  13. {

  14. string xml = "<Test><Name>Test class</Name><X>100</X><Y>200</Y></Test>";

  15.  
  16. XmlDocument doc = new XmlDocument();

  17. doc.LoadXml(xml);

  18. string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);

  19.  
  20. Console.WriteLine("XML -> JSON: {0}", json);

  21. Console.ReadLine();

  22.  
  23. }

  24. }

  25. }


json2xml

 預定義的Json字符串如上

  同理調用Json.Net類庫中的方法

  XmlDocument doc1 = JsonConvert.DeserializeXmlNode(json);

  Console.WriteLine(doc1.OuterXml);

 

XmlDocument doc2 = JsonConvert.DeserializeXmlNode(json1);

  Console.WriteLine(doc2.OuterXml);

http://dotnet.chinaitlab.com/CSharp/927201.html

 

http://www.tuicool.com/articles/n2Uzya

 


 
  1. XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(sJson), XmlDictionaryReaderQuotas.Max);

  2. XmlDocument doc = new XmlDocument();

  3. doc.Load(reader);

  4.  
  5. 收藏一下吧 以後萬一用的到

  6.  
  7. 我試了一下,json字符串需要把鍵加"纔行

  8. 如:{Name:"aaa",Value:1} 這裏Name和Value是鍵這樣寫法轉換的時候報錯

  9. 需要寫成

  10. {"Name":"aaa","Value":1}

  11.  
  12.  
  13.  
  14.  
  15.  
  16. 這個是第二種方法,這個鍵加不加"都正常翻譯

  17.  
  18. /// <summary>

  19. /// json字符串轉換爲Xml對象

  20. /// </summary>

  21. /// <param name="sJson"></param>

  22. /// <returns></returns>

  23. public static XmlDocument Json2Xml(string sJson)

  24. {

  25. //XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(sJson), XmlDictionaryReaderQuotas.Max);

  26. //XmlDocument doc = new XmlDocument();

  27. //doc.Load(reader);

  28.  
  29. JavaScriptSerializer oSerializer = new JavaScriptSerializer();

  30. Dictionary<string, object> Dic = (Dictionary<string, object>)oSerializer.DeserializeObject(sJson);

  31. XmlDocument doc = new XmlDocument();

  32. XmlDeclaration xmlDec;

  33. xmlDec = doc.CreateXmlDeclaration("1.0", "gb2312", "yes");

  34. doc.InsertBefore(xmlDec, doc.DocumentElement);

  35. XmlElement nRoot = doc.CreateElement("root");

  36. doc.AppendChild(nRoot);

  37. foreach (KeyValuePair<string, object> item in Dic)

  38. {

  39. XmlElement element = doc.CreateElement(item.Key);

  40. KeyValue2Xml(element, item);

  41. nRoot.AppendChild(element);

  42. }

  43. return doc;

  44. }

  45.  
  46. private static void KeyValue2Xml(XmlElement node, KeyValuePair<string, object> Source)

  47. {

  48. object kValue = Source.Value;

  49. if (kValue.GetType() == typeof(Dictionary<string, object>))

  50. {

  51. foreach (KeyValuePair<string, object> item in kValue as Dictionary<string, object>)

  52. {

  53. XmlElement element = node.OwnerDocument.CreateElement(item.Key);

  54. KeyValue2Xml(element, item);

  55. node.AppendChild(element);

  56. }

  57. }

  58. else if (kValue.GetType() == typeof(object[]))

  59. {

  60. object[] o = kValue as object[];

  61. for (int i = 0; i < o.Length; i++)

  62. {

  63. XmlElement xitem = node.OwnerDocument.CreateElement("Item");

  64. KeyValuePair<string, object> item = new KeyValuePair<string, object>("Item", o[i]);

  65. KeyValue2Xml(xitem, item);

  66. node.AppendChild(xitem);

  67. }

  68.  
  69. }

  70. else

  71. {

  72. XmlText text = node.OwnerDocument.CreateTextNode(kValue.ToString());

  73. node.AppendChild(text);

  74. }

  75. }

  76.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章