http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 32151|回复: 9

[综合] C#Json转Xml格式数据的方法总结

[复制链接]
发表于 2013-4-27 15:08:28 | 显示全部楼层 |阅读模式
第一种方法,也是我用的最多的一种,很简单方法如下
[C#] 纯文本查看 复制代码
 string Xml = "在这里写Json字符串";
            XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(Xml), XmlDictionaryReaderQuotas.Max);
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            Xml = doc.InnerXml;

Xml就是我们要的数据
XmlDictionaryReader 需要我们添加引用,如下
QQ截图20130427150713.jpg
然后就可以直接使用了,非常的方便 ,
另个一种方法如下
[C#] 纯文本查看 复制代码
/// <summary>
        /// json字符串转换为Xml对象
        /// </summary>
        /// <param name="sJson"></param>
        /// <returns></returns>
        public static XmlDocument Json2Xml(string sJson)
        {
            //XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(sJson), XmlDictionaryReaderQuotas.Max);
            //XmlDocument doc = new XmlDocument();
            //doc.Load(reader);

            JavaScriptSerializer oSerializer = new JavaScriptSerializer();
            Dictionary<string, object> Dic = (Dictionary<string, object>)oSerializer.DeserializeObject(sJson);
            XmlDocument doc = new XmlDocument();
            XmlDeclaration xmlDec;
            xmlDec = doc.CreateXmlDeclaration("1.0", "gb2312", "yes");
            doc.InsertBefore(xmlDec, doc.DocumentElement);
            XmlElement nRoot = doc.CreateElement("root");
            doc.AppendChild(nRoot);
            foreach (KeyValuePair<string, object> item in Dic)
            {
                XmlElement element = doc.CreateElement(item.Key);
                KeyValue2Xml(element, item);
                nRoot.AppendChild(element);
            }
            return doc;
        }

        private static void KeyValue2Xml(XmlElement node, KeyValuePair<string, object> Source)
        {
            object kValue = Source.Value;
            if (kValue.GetType() == typeof(Dictionary<string, object>))
            {
                foreach (KeyValuePair<string, object> item in kValue as Dictionary<string, object>)
                {
                    XmlElement element = node.OwnerDocument.CreateElement(item.Key);
                    KeyValue2Xml(element, item);
                    node.AppendChild(element);
                }
            }
            else if (kValue.GetType() == typeof(object[]))
            {
                object[] o = kValue as object[];
                for (int i = 0; i < o.Length; i++)
                {
                    XmlElement xitem = node.OwnerDocument.CreateElement("Item");
                    KeyValuePair<string, object> item = new KeyValuePair<string, object>("Item", o);
                    KeyValue2Xml(xitem, item);
                    node.AppendChild(xitem);
                }
                  
            }
            else
            {
                XmlText text = node.OwnerDocument.CreateTextNode(kValue.ToString());
                node.AppendChild(text);
            }
        }

说真的这种方法我在项目中没有真的使用过,也是网上看的,大家如果有兴趣的话可以测试一下。

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-6-29 15:59:35 | 显示全部楼层
{"result": {"total_results": 7, "car_lists": [{"model_2": "CM2_05", "plate_number": "\u95fdCRA262", "expected_flow": "", "driver_gender": "", "description": "", "locate_status": 1, "model_3": "", "located_time": "", "driver_id_number": "", "volume": "", "width": "", "length": 4.2000000000000002, "driver_name": "\u5f90", "model_1": "CM1_01", "location": "", "tonnage": 11.0, "locate_number": "13850754219", "height": "", "model_4": ""}, {"model_2": "CM2_13", "plate_number": "\u5409A12311", "expected_flow": "\u4e0a\u6d77\u5e02,\u676d\u5dde\u5e02,\u6d4e\u5357\u5e02", "driver_gender": "", "description": "fddsfs", "locate_status": 0, "model_3": "", "located_time": "", "driver_id_number": "", "volume": "", "width": "", "length": 12.0, "driver_name": "13fsdfsd", "model_1": "CM1_16", "location": "", "tonnage": 111.0, "locate_number": "15659826026", "height": "", "model_4": ""}, {"model_2": "CM2_13", "plate_number": "\u95fdC12345", "expected_flow": "", "driver_gender": "", "description": "", "locate_status": 0, "model_3": "", "located_time": "", "driver_id_number": "", "volume": "", "width": "", "length": 13.0, "driver_name": "xur", "model_1": "CM1_13", "location": "", "tonnage": 39.0, "locate_number": "13787903987", "height": "", "model_4": ""}, {"model_2": "CM2_14", "plate_number": "\u6842A12345", "expected_flow": "", "driver_gender": "", "description": "\u6d4b\u8bd5\u4fee\u6539\u4fe1\u606f\uff01", "locate_status": 0, "model_3": "", "located_time": "", "driver_id_number": "", "volume": 26.0, "width": "", "length": 15.0, "driver_name": "\u674e\u5927\u80c63", "model_1": "CM1_14", "location": "", "tonnage": 44.0, "locate_number": "13737173444", "height": "", "model_4": ""}, {"model_2": "CM2_16", "plate_number": "\u6e58A22224", "expected_flow": "\u4e0a\u6d77\u5e02", "driver_gender": "", "description": "", "locate_status": 0, "model_3": "", "located_time": "", "driver_id_number": "", "volume": "", "width": "", "length": "", "driver_name": "\u53cd\u590d\u53cd\u590d", "model_1": "CM1_17", "location": "", "tonnage": "", "locate_number": "15606950023", "height": "", "model_4": ""}, {"model_2": "CM2_13", "plate_number": "\u6d25B12412", "expected_flow": "\u5e7f\u5dde\u5e02", "driver_gender": "", "description": "", "locate_status": 0, "model_3": "", "located_time": "", "driver_id_number": "", "volume": "", "width": "", "length": "", "driver_name": "\u65b9\u82b3\u82b3", "model_1": "CM1_17", "location": "", "tonnage": "", "locate_number": "15306962033", "height": "", "model_4": ""}, {"model_2": "CM2_01", "plate_number": "\u6842A16880", "expected_flow": "", "driver_gender": "", "description": "\u6d4b\u8bd5\u6dfb\u52a0\u8f66\u8f86\u4fe1\u606f", "locate_status": 0, "model_3": "", "located_time": "", "driver_id_number": "", "volume": 100.0, "width": "", "length": 10.0, "driver_name": "\u52d2\u5e03\u6717", "model_1": "CM1_10", "location": "", "tonnage": 50.0, "locate_number": "13737114377", "height": "", "model_4": ""}], "register_contact_number": "13850754219"}, "success": true}
象这种格式的json怎么解析啊
 楼主| 发表于 2013-12-11 15:32:57 | 显示全部楼层
kkk 发表于 2013-6-29 15:59
{"result": {"total_results": 7, "car_lists": [{"model_2": "CM2_05", "plate_number": "\u95fdCRA262",  ...

一样的方法啊,转成Xml直接取值就行
发表于 2015-2-28 13:41:33 | 显示全部楼层
受教了,解析json先转化为XML,然后在进行。。。感谢
发表于 2015-12-9 23:19:08 | 显示全部楼层
你上面这段代码有一个bug
在第48行中的 KeyValuePair<string, object> item = new KeyValuePair<string, object>("Item", o);这句
这样传入会导致死循环,正确的应该是
KeyValuePair<string, object> item = new KeyValuePair<string, object>("Item", o[i]);
发表于 2015-12-9 23:42:32 | 显示全部楼层
你上面的代码中还有一个问题就是在KeyValue2Xml函数中的Source和Source.Value的没判断是否为null,而实际从json传入的内容是完全可能为null的;
发表于 2017-6-16 16:20:17 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-6-5 14:11:12 | 显示全部楼层
我只是路过打酱油的。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-22 08:08

© 2014-2021

快速回复 返回顶部 返回列表