• 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
  • 相关阅读:
    基于pandas索引的数据查找、排序和去重小结
    python中的变量引用小结
    python中日志logging模块和异常捕获traceback模块的使用
    undefined和not defined
    spring 整合dubbo session中调用用户转换异常 classcastEcxeption
    iframe占满整个屏幕
    freemarker获取url中参数
    H5的新增标签
    Redis
    类加载过程
  • 原文地址:https://www.cnblogs.com/1003487863qq/p/3450698.html
Copyright © 2020-2023  润新知