ASP.NET中XML转JSON的方法
许多应用程序都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理。要实现这一点,它们必须将XML格式转换为JSON格式。
XML转JSON代码
- private 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();
- }
ASP.NET中XML转JSON的方法的更多相关文章
- ASP.net中导出Excel的简单方法介绍
下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...
- xml转json的方法
.第一种方法 使用JSON-JAVA提供的方法,之前一直使用json-lib提供的方法转json,后来发现了这个开源项目,觉得用起来很不错,并且可以修改XML.java中的parse方法满足自己的转换 ...
- C#中XML和json互相转换
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xm ...
- asp.net中获取当前url的方法
HttpContext.Current.Request.Url.ToString() 并不可靠. 如果当前URL为 http://localhost/search.aspx?user=http://c ...
- 在Asp.Net中使用SmtpMail发送邮件的方法
在ASP中,就可以通过调用CDONTS组件发送简单邮件,在ASP.Net中,自然也可以.不同的是,.Net Framework中,将这一组件封装到了System.Web.Mail命名空间中. 一个典型 ...
- 慎重Asp.net中static变量的使用方法
在.Net平台下进行CS软件开发时,我们常常遇到以后还要用到某些变量上次改动后的值,为了简单起见,非常多人都习惯用static来定义这些变量,我也是.这样非常方便.下一次调用某个函数时该变量仍然保存的 ...
- java中常见的json解析方法、库以及性能对比
常见的json解析有原生的JSONObject和JSONArray方法,谷歌的GSON库,阿里的fastjson,还有jackson,json-lib. Gson(项目地址:https://githu ...
- java 中xml转换为json对象
1.前提须要jar包: json-lib-2.4-jdk15.jar 和 xom-1.2.5.jar ,maven 仓库: net.sf.json-lib json-lib 2.4 jdk15 xom ...
- java中Xml、json之间的相互转换
旁白: 最近关于xml与json之间的转换都搞蒙了,这里写一个demo,以后备用. 正题: project格式是: jar包是一个一个检出来的,还算干净了. 代码: 工具类: package exer ...
随机推荐
- ASP.NET 5 Beta8 发布
ASP.NET 5 Beta8 发布 ASP.NET 5 的路线图(详见 ASP.NET 5 Schedule and Roadmap : https://github.com/aspnet/home ...
- Gradle sourceCompatibility has no effect to subprojects(转)
I have Java 6 and 7 installed on my machine. Gradle uses 1.7 (checked using gradle -v). But I need t ...
- 【Linux命令】--(9)其他常用命令
其他常用命令+++++++++++++++++++++++++++++++lndiffdatecal grep wcpswatchatcrontab++++++++++++++++++++++++++ ...
- cocos2d-html5基金会
1 环境结构 版本号Cocos2d-html5-v2.2,tomcat7.0 构造tomcat.然后直接解压Cocos2d-html5-v2.2.zip.解压后根文件访问的文件夹index.html你 ...
- php用魔术方法__call实现类函数重载
因为php是弱类型语言,不喜欢c++通过改变函数返回相同的值键入的参数的数目和功能将过载!但在需求函数的实际发展可能过载.开发需求,我们能够通过魔术方法__call()来实现函数重载. class T ...
- 他们控制的定义--让背景颜色变化ViewPager逐步幻灯片
转载请注明出处.谢谢~ 今天想说一个简单但很好的效果达到.代码是绝对简单,达到绝对easy,就是你可能想不到而已. 不多说,上效果图. 第一个效果是仿最美应用的滑动颜色变化,第二个是我项目中要用的效果 ...
- C# 你不能调用的问题剪贴板线程
最近在做一个项目,需要使用线程,并使用剪贴板,头发得到较少的数据在剪贴板上后,现在的孩子线程创建一个子线程,我特别困惑,上网查资料.最后,得到最终的.下面的例子现在将概括解: 第一步: public ...
- Same binary weight (位运算)
题目描述 The binary weight of a positive integer is the number of 1's in its binary representation.for ...
- linux_UBUNTU 12.04 上使用 SQUID 架设HTTP正向代理服务器
配置普通HTTP正向代理 安装 1 sudo apt-get install squid squid-common 配置 squid3 1 sudo vim /etc/squid3/squid ...
- api接口对于客户端的身份认证方式以及安全措施
转载 基于http协议的api接口对于客户端的身份认证方式以及安全措施 由于http是无状态的,所以正常情况下在浏览器浏览网页,服务器都是通过访问者的cookie(cookie中存储的jsession ...