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(); } } }
查看全文
相关阅读:
设计模式学习--Singleton
Add Binary
简洁的ios小界面
第一节、介绍
魅族MX5和努比亚布拉格手机參数对照
python 深浅拷贝 进阶
为什么要重写equals()方法与hashCode()方法
在Myeclipse buildpath 加server lib
push本地代码到github出错
mysql事务,select for update,及数据的一致性处理
原文地址:https://www.cnblogs.com/zhangqs008/p/2341092.html
最新文章
细数判断数据类型的各种方法
React创建组件的三种方式及其区别
js实现复制粘贴功能
交互式数据可视化-D3.js(四)形状生成器
为公司架构一套高质量的 Vue UI 组件库
几个非常实用的JQuery代码片段
ZOJ 3829 模拟贪心
Swift基础--定位
Picking up Jewels
任务调度器quartz的使用
热门文章
使用Java语言实现,自己主动生成10个整数(1~100,求出生成数列中的最大值和最小值,不同意使用Arrays类的sort方法
pcap网络抓包 无法import pcap
叫号系统排队系统挂号系统实现(JAVA队列)
debian mysql 定时自己主动备份的脚本
php 二维数组传递给 js 问题解决记录
非典型的scala程序及其编译后的结果
Qt5官方demo解析集28——Extending QML
Gradle 1.12 翻译——第十五章. 任务详述
机器学习系列(5)_从白富美相亲看特征预处理与选择(上)
学习easyui疑问(三)
Copyright © 2011-2022 走看看