XmlToJson.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. namespace Ant.Service.Common
  8. {
  9. /// <summary>
  10. /// 将xml转换为json
  11. /// </summary>
  12. public class XmlToJson
  13. {
  14. public static string XmlStrToJSON(string xmlStr)
  15. {
  16. XmlDocument doc = new XmlDocument();
  17. doc.LoadXml(xmlStr);
  18. return XmlToJSON(doc);
  19. }
  20. /// <summary>
  21. /// 将xml转换为json
  22. /// </summary>
  23. /// <param name="xmlFile">xml文件</param>
  24. /// <returns></returns>
  25. public static string XmlToJSON(string xmlFile)
  26. {
  27. XmlDocument doc = new XmlDocument();
  28. doc.Load(xmlFile);
  29. return XmlToJSON(doc);
  30. }
  31. /// <summary>
  32. /// 将xml转换为json
  33. /// </summary>
  34. /// <param name="xmlDoc">xml文档</param>
  35. /// <returns></returns>
  36. public static string XmlToJSON(XmlDocument xmlDoc)
  37. {
  38. StringBuilder sbJSON = new StringBuilder();
  39. sbJSON.Append("{ ");
  40. XmlToJSONnode(sbJSON, xmlDoc.DocumentElement, true);
  41. sbJSON.Append("}");
  42. return sbJSON.ToString();
  43. }
  44. // XmlToJSONnode: Output an XmlElement, possibly as part of a higher array
  45. private static void XmlToJSONnode(StringBuilder sbJSON, XmlElement node, bool showNodeName)
  46. {
  47. if (showNodeName)
  48. sbJSON.Append("\"" + SafeJSON(node.Name) + "\": ");
  49. sbJSON.Append("{");
  50. // Build a sorted list of key-value pairs
  51. // where key is case-sensitive nodeName
  52. // value is an ArrayList of string or XmlElement
  53. // so that we know whether the nodeName is an array or not.
  54. SortedList childNodeNames = new SortedList();
  55. // Add in all node attributes
  56. if (node.Attributes != null)
  57. foreach (XmlAttribute attr in node.Attributes)
  58. StoreChildNode(childNodeNames, attr.Name, attr.InnerText);
  59. // Add in all nodes
  60. foreach (XmlNode cnode in node.ChildNodes)
  61. {
  62. if (cnode is XmlText)
  63. StoreChildNode(childNodeNames, "value", cnode.InnerText);
  64. else if (cnode is XmlElement)
  65. StoreChildNode(childNodeNames, cnode.Name, cnode);
  66. }
  67. // Now output all stored info
  68. foreach (string childname in childNodeNames.Keys)
  69. {
  70. ArrayList alChild = (ArrayList)childNodeNames[childname];
  71. if (alChild.Count == 1)
  72. OutputNode(childname, alChild[0], sbJSON, true);
  73. else
  74. {
  75. sbJSON.Append(" \"" + SafeJSON(childname) + "\": [ ");
  76. foreach (object Child in alChild)
  77. OutputNode(childname, Child, sbJSON, false);
  78. sbJSON.Remove(sbJSON.Length - 2, 2);
  79. sbJSON.Append(" ], ");
  80. }
  81. }
  82. sbJSON.Remove(sbJSON.Length - 2, 2);
  83. sbJSON.Append(" }");
  84. }
  85. // StoreChildNode: Store data associated with each nodeName
  86. // so that we know whether the nodeName is an array or not.
  87. private static void StoreChildNode(SortedList childNodeNames, string nodeName, object nodeValue)
  88. {
  89. // Pre-process contraction of XmlElement-s
  90. if (nodeValue is XmlElement)
  91. {
  92. // Convert <aa></aa> into "aa":null
  93. // <aa>xx</aa> into "aa":"xx"
  94. XmlNode cnode = (XmlNode)nodeValue;
  95. if (cnode.Attributes.Count == 0)
  96. {
  97. XmlNodeList children = cnode.ChildNodes;
  98. if (children.Count == 0)
  99. nodeValue = null;
  100. else if (children.Count == 1 && (children[0] is XmlText))
  101. nodeValue = ((XmlText)(children[0])).InnerText;
  102. }
  103. }
  104. // Add nodeValue to ArrayList associated with each nodeName
  105. // If nodeName doesn't exist then add it
  106. object oValuesAL = childNodeNames[nodeName];
  107. ArrayList ValuesAL;
  108. if (oValuesAL == null)
  109. {
  110. ValuesAL = new ArrayList();
  111. childNodeNames[nodeName] = ValuesAL;
  112. }
  113. else
  114. ValuesAL = (ArrayList)oValuesAL;
  115. ValuesAL.Add(nodeValue);
  116. }
  117. private static void OutputNode(string childname, object alChild, StringBuilder sbJSON, bool showNodeName)
  118. {
  119. if (alChild == null)
  120. {
  121. if (showNodeName)
  122. sbJSON.Append("\"" + SafeJSON(childname) + "\": ");
  123. sbJSON.Append("null");
  124. }
  125. else if (alChild is string)
  126. {
  127. if (showNodeName)
  128. sbJSON.Append("\"" + SafeJSON(childname) + "\": ");
  129. string sChild = (string)alChild;
  130. sChild = sChild.Trim();
  131. sbJSON.Append("\"" + SafeJSON(sChild) + "\"");
  132. }
  133. else
  134. XmlToJSONnode(sbJSON, (XmlElement)alChild, showNodeName);
  135. sbJSON.Append(", ");
  136. }
  137. // Make a string safe for JSON
  138. private static string SafeJSON(string sIn)
  139. {
  140. StringBuilder sbOut = new StringBuilder(sIn.Length);
  141. foreach (char ch in sIn)
  142. {
  143. if (Char.IsControl(ch) || ch == '\'')
  144. {
  145. int ich = (int)ch;
  146. sbOut.Append(@"\u" + ich.ToString("x4"));
  147. continue;
  148. }
  149. else if (ch == '\"' || ch == '\\' || ch == '/')
  150. {
  151. sbOut.Append('\\');
  152. }
  153. sbOut.Append(ch);
  154. }
  155. return sbOut.ToString();
  156. }
  157. }
  158. }