zoukankan      html  css  js  c++  java
  • C#Json转Xml格式数据的方法

    第一种方法

    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 需要我们添加引用,如下
    <ignore_js_op> 

    QQ截图20130427150713.jpg (36.66 KB, 下载次数: 64)

    下载附件

    2013-4-27 15:07 上传

     


    然后就可以直接使用了,非常的方便 ,
    另个一种方法如下

    /// <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);
                }
            }
  • 相关阅读:
    加沙地带
    特拉维夫以色列第二大城市,滨临东地中海,以色列最为国际化的经济中心
    1980年,以色列国会立法确定耶路撒冷是该国“永远的与不可分割的首都”。而巴勒斯坦自治政府也宣布耶路撒冷将是未来巴勒斯坦国的首都。在21世纪,耶路撒冷仍然是巴以冲突的中心。
    delete
    NUnit -- Test discovery or execution might not work for this project
    HearthBuddy中_settings.txt的更详细参数解释
    WPF global exception handler
    sftp winscp
    cdn and fallback
    What happens in an async method
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/7111998.html
Copyright © 2011-2022 走看看