zoukankan
html css js c++ java
XmlToJsonxml对象转换为Json对象类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Collections; namespace Framework { /// <summary> /// 将xml转换为json /// </summary> public class XmlToJson { /// <summary> /// 将xml转换为json /// </summary> /// <param name="xmlFile">xml文件</param> /// <returns></returns> public static string XmlToJSON(string xmlFile) { XmlDocument doc = new XmlDocument(); doc.Load(xmlFile); return XmlToJSON(doc); } /// <summary> /// 将xml转换为json /// </summary> /// <param name="xmlDoc">xml文档</param> /// <returns></returns> public static string XmlToJSON(XmlDocument xmlDoc) { StringBuilder sbJSON = new StringBuilder(); sbJSON.Append("{ "); XmlToJSONnode(sbJSON, xmlDoc.DocumentElement, true); sbJSON.Append("}"); return sbJSON.ToString(); } // XmlToJSONnode: Output an XmlElement, possibly as part of a higher array private static void XmlToJSONnode(StringBuilder sbJSON, XmlElement node, bool showNodeName) { if (showNodeName) sbJSON.Append("\"" + SafeJSON(node.Name) + "\": "); sbJSON.Append("{"); // Build a sorted list of key-value pairs // where key is case-sensitive nodeName // value is an ArrayList of string or XmlElement // so that we know whether the nodeName is an array or not. SortedList childNodeNames = new SortedList(); // Add in all node attributes if (node.Attributes != null) foreach (XmlAttribute attr in node.Attributes) StoreChildNode(childNodeNames, attr.Name, attr.InnerText); // Add in all nodes foreach (XmlNode cnode in node.ChildNodes) { if (cnode is XmlText) StoreChildNode(childNodeNames, "value", cnode.InnerText); else if (cnode is XmlElement) StoreChildNode(childNodeNames, cnode.Name, cnode); } // Now output all stored info foreach (string childname in childNodeNames.Keys) { ArrayList alChild = (ArrayList)childNodeNames[childname]; if (alChild.Count == 1) OutputNode(childname, alChild[0], sbJSON, true); else { sbJSON.Append(" \"" + SafeJSON(childname) + "\": [ "); foreach (object Child in alChild) OutputNode(childname, Child, sbJSON, false); sbJSON.Remove(sbJSON.Length - 2, 2); sbJSON.Append(" ], "); } } sbJSON.Remove(sbJSON.Length - 2, 2); sbJSON.Append(" }"); } // StoreChildNode: Store data associated with each nodeName // so that we know whether the nodeName is an array or not. private static void StoreChildNode(SortedList childNodeNames, string nodeName, object nodeValue) { // Pre-process contraction of XmlElement-s if (nodeValue is XmlElement) { // Convert <aa></aa> into "aa":null // <aa>xx</aa> into "aa":"xx" XmlNode cnode = (XmlNode)nodeValue; if (cnode.Attributes.Count == 0) { XmlNodeList children = cnode.ChildNodes; if (children.Count == 0) nodeValue = null; else if (children.Count == 1 && (children[0] is XmlText)) nodeValue = ((XmlText)(children[0])).InnerText; } } // Add nodeValue to ArrayList associated with each nodeName // If nodeName doesn't exist then add it object oValuesAL = childNodeNames[nodeName]; ArrayList ValuesAL; if (oValuesAL == null) { ValuesAL = new ArrayList(); childNodeNames[nodeName] = ValuesAL; } else ValuesAL = (ArrayList)oValuesAL; ValuesAL.Add(nodeValue); } private static void OutputNode(string childname, object alChild, StringBuilder sbJSON, bool showNodeName) { if (alChild == null) { if (showNodeName) sbJSON.Append("\"" + SafeJSON(childname) + "\": "); sbJSON.Append("null"); } else if (alChild is string) { if (showNodeName) sbJSON.Append("\"" + SafeJSON(childname) + "\": "); string sChild = (string)alChild; sChild = sChild.Trim(); sbJSON.Append("\"" + SafeJSON(sChild) + "\""); } else XmlToJSONnode(sbJSON, (XmlElement)alChild, showNodeName); sbJSON.Append(", "); } // Make a string safe for JSON private static string SafeJSON(string sIn) { StringBuilder sbOut = new StringBuilder(sIn.Length); foreach (char ch in sIn) { if (Char.IsControl(ch) || ch == '\'') { int ich = (int)ch; sbOut.Append(@"\u" + ich.ToString("x4")); continue; } else if (ch == '\"' || ch == '\\' || ch == '/') { sbOut.Append('\\'); } sbOut.Append(ch); } return sbOut.ToString(); } } }
查看全文
相关阅读:
Phalanx--hdu2859(dp 最大对称子图)
Spring Cloud-hystrix Dashboard(八)
Spring Cloud-hystrix使用例子(七)
mysql deadlock、Lock wait timeout解决和分析
Spring Cloud-hystrix(六)
Spring Cloud-Ribbon负载均衡策略类IRule(五)
Spring Cloud-Ribbon ILoadBalancer负载均衡器核心源码(四)
Spring Cloud-Ribbon实现客户端的服务均衡(三)
Spring Cloud-Eureka实现服务的注册与发现(二)
Spring Cloud-个人理解的微服务演变过程(一)
原文地址:https://www.cnblogs.com/zhangqs008/p/2341092.html
最新文章
L1-5. A除以B【一种输出格式错了,务必看清楚输入输出】
poj 2506 Tiling 递推
poj 2299 Ultra-QuickSort 归并排序求逆序数对
poj 2083 Fractal 递归 图形打印
poj 1164 The Castle
poj 2109 Power of Cryptography
poj 2965 The Pilots Brothers' refrigerator
hdu 1466 计算直线的交点数 递推
hdu 1178 Heritage from father (推导)
hdu 1044 Collect More Jewels
热门文章
hdu 1025 Constructing Roads In JGShining's Kingdom
Gray code---hdu5375(格雷码与二进制码,普通dp)
Dire Wolf ---hdu5115(区间dp)
Card Game Cheater---hdu1528(扑克建图求二分匹配)
Knight's Trip---hdu3766(马走日求最小走的步数)
LightOj 1245 --- Harmonic Number (II)找规律
lines---hdu5124(离散化+数组模拟)
Bridging signals---hdu1950(最长上升子序列复杂度n*log(n) )
Help Jimmy--poj1661(dp)
生活是我们自己的事情
Copyright © 2011-2022 走看看