public static XmlDocument Json2Xml2(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); } } /// <summary> /// 把Json转Xml /// </summary> /// <param name="json">源JSON字符串</param> public static XmlDocument JsonToXml(string json) { //递归遍历JSON中的项并写入到XML中 JToken jp = JToken.Parse(json); 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); if (jp.Type == JTokenType.Object) //JSON为一个Object的情况 { AnalyseJsonObject(doc, nRoot, jp); } else if (jp.Type == JTokenType.Array) //JSON为一个Array的情况 { AnalyseJsonArray(doc, nRoot, jp); } return doc; } /// <summary> /// 将一段JSON格式字符串写入到XML文件中 /// </summary> /// <param name="json">JSON字符串</param> public static void AnalyseJsonObject(XmlDocument doc,XmlElement node, JToken jt) { //遍历读取到的JSON字符串 foreach (JProperty jtemp in jt.Children()) { //根据读入的不同数据类型分类讨论 switch (jtemp.Value.Type) { case JTokenType.Object: { XmlElement element = doc.CreateElement(jtemp.Name); AnalyseJsonObject(doc,element, jtemp); node.AppendChild(element); } break; case JTokenType.Array: { XmlElement element = doc.CreateElement(jtemp.Name); AnalyseJsonArray(doc, element, jtemp.Value); node.AppendChild(element); } break; case JTokenType.String: case JTokenType.Boolean: case JTokenType.Integer: case JTokenType.Float: default: { XmlText text = node.OwnerDocument.CreateTextNode(jtemp.Value.ToString()); node.AppendChild(text); } break; } } } /// <summary> /// 将一段JSON格式Array内容写入到XML文件中 /// </summary> /// <param name="writer">XML编写器</param> /// <param name="json">JSON字符串</param> public static void AnalyseJsonArray(XmlDocument doc, XmlElement node, JToken jt) { foreach (JProperty jtemp in jt) { switch (jtemp.Type) { case JTokenType.Object: { XmlElement element = doc.CreateElement(jtemp.Name); AnalyseJsonObject(doc, element, jtemp); node.AppendChild(element); } break; case JTokenType.Array: { XmlElement element = doc.CreateElement(jtemp.Name); AnalyseJsonArray(doc, element, jtemp); node.AppendChild(element); } break; case JTokenType.String: case JTokenType.Boolean: case JTokenType.Integer: case JTokenType.Float: default: { XmlText text = node.OwnerDocument.CreateTextNode(jtemp.Value.ToString()); node.AppendChild(text); } break; } } }