zoukankan      html  css  js  c++  java
  • jansen字符串转换为xml格式

       /// <summary>
            /// json字符串转换为Xml对象
            /// </summary>
            /// <param name="sJson"></param>
            /// <returns></returns>
            public static XmlDocument Json2Xml(string sJson)
            {
                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", "utf-8", "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[i]);
                        KeyValue2Xml(xitem, item);
                        node.AppendChild(xitem);
                    }
    
                }
                else
                {
                    XmlText text = node.OwnerDocument.CreateTextNode(kValue.ToString());
                    node.AppendChild(text);
                }
            }
    View Code
  • 相关阅读:
    ORA-22835:缓冲区对于CLOB到CHAR转换而言太小
    C#发起Http请求,调用接口
    C#发起HTTP请求Post请求
    C# 调用HTTP接口两种方式Demo WebRequest/WebResponse 和WebApi
    SQL中的子查询
    C# 使用multipart form-data方式post数据到服务器
    批处理框架 Spring Batch 这么强,你会用吗
    JAVA基础(一)
    数据库---连接查询多表查询
    数据库---约束
  • 原文地址:https://www.cnblogs.com/1003487863qq/p/3450698.html
Copyright © 2011-2022 走看看