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
  • 相关阅读:
    Codeforces Round #380(div 2)
    Codeforces Round #378(div 2)
    Codeforces Round #379(div 2)
    CCPC2016合肥现场赛
    CCPC2016沈阳站
    HDU2222 Keywords Search__AC自动机
    poj2185Milking Grid
    POJ2961_kmp
    POJ 2406
    poj 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/1003487863qq/p/3450698.html
Copyright © 2011-2022 走看看