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.  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值