解决方案 »

  1.   

    去下载Newtonsoft.Json.dll研究下,只要能解析json,转成什么格式,你随意
      

  2.   

    Json的结构定义的不错,解析如下,需要引用json.NetJsonClass m = JsonConvert.DeserializeObject<JsonClass>(File.ReadAllText("Json.Txt"));
    .......    public class JsonClass
        {
            public  Head head { get; set; }
            public  Body body { get; set; }
        }    public class Head
        {
            public string applyDate { get; set; }
            public string service { get; set; }
            public string version { get; set; }
            public string system { get; set; }
            public int applyID { get; set; }
            public bool error { get; set; }
            public int replyID { get; set; }
            public string replyDate { get; set; }
        }    public class Body
        {
            public int Status { get; set; }
            public string Error { get; set; }        
            public List<Test> List { get; set; }
        }    
        public class Test
        {
            public int TestId { get; set; }
            public string Type { get; set; }
            public string Name { get; set; }
        }
    Xml的定义比较差。你可以自己用XmlDocument或者Xdocument生成。
      

  3.   

    试试fastCSharp,下面的代码没有处理XML编码转义(比如<>...)
            struct xml
            {
                public fastCSharp.charStream Stream;
                public string FromJson(string json)
                {
                    fastCSharp.setup.cSharp.json.node boot = new fastCSharp.setup.cSharp.json.parser().Parse(json);
                    Stream.Write(@"<?xml version=""1.0"" encoding=""utf-8""?><apply>");
                    foreach (System.Collections.Generic.KeyValuePair<string, fastCSharp.setup.cSharp.json.node> node in boot.Dictionary)
                    {
                        Stream.Write("<" + node.Key + ">");
                        set(node.Value);
                        Stream.Write("</" + node.Key + ">");
                    }
                    Stream.Write("</apply>");
                    return Stream.ToString();
                }
                private void set(fastCSharp.setup.cSharp.json.node boot)
                {
                    switch (boot.Type)
                    {
                        case fastCSharp.setup.cSharp.json.node.nodeType.Dictionary:
                            foreach (System.Collections.Generic.KeyValuePair<string, fastCSharp.setup.cSharp.json.node> node in boot.Dictionary)
                            {
                                Stream.Write(@"<set name=""" + node.Key);
                                if (node.Value.Type == fastCSharp.setup.cSharp.json.node.nodeType.List) Stream.Write(@""" type=""array");
                                Stream.Write(@""">");
                                set(node.Value);
                                Stream.Write("</set>");
                            }
                            break;
                        case fastCSharp.setup.cSharp.json.node.nodeType.List:
                            foreach (fastCSharp.setup.cSharp.json.node node in boot.List) set(node);
                            break;
                        case fastCSharp.setup.cSharp.json.node.nodeType.Null:
                            Stream.Write(fastCSharp.web.ajax.Null);
                            break;
                        case fastCSharp.setup.cSharp.json.node.nodeType.DoubleSubString:
                        case fastCSharp.setup.cSharp.json.node.nodeType.SingleSubString:
                        case fastCSharp.setup.cSharp.json.node.nodeType.String:
                            Stream.Write(boot.String);
                            break;
                        case fastCSharp.setup.cSharp.json.node.nodeType.Int:
                            Stream.Write(boot.Int.ToString());
                            break;
                        case fastCSharp.setup.cSharp.json.node.nodeType.Number:
                            Stream.Write(boot.Number);
                            break;
                        case fastCSharp.setup.cSharp.json.node.nodeType.False:
                        case fastCSharp.setup.cSharp.json.node.nodeType.True:
                            Stream.Write(boot.Bool.ToString());
                            break;
                        case fastCSharp.setup.cSharp.json.node.nodeType.DateTime:
                            Stream.Write(boot.DateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                            break;
                    }
                }
            }
                using (fastCSharp.charStream stream = new fastCSharp.charStream())
                {
                    Console.WriteLine(new xml { Stream = stream }.FromJson(json));
                }
      

  4.   

     using System;
    using System.Collections;
    using System.Globalization;
    using System.Text;
    using System.Xml;
    namespace COM.M.N
    {
        public class XmlToJSONParser
        {
            private static void OutputNode(string childname, object alChild, StringBuilder sbJSON, bool showNodeName)
            {
                if (alChild == null)
                {
                    if (showNodeName)
                    {
                        sbJSON.Append(SafeJSON(childname) + ": ");
                    }
                    sbJSON.Append("null");
                }
                else if (alChild is string)
                {
                    if (showNodeName)
                    {
                        sbJSON.Append(SafeJSON(childname) + ": ");
                    }
                    string s = (string)alChild;
                    s = s.Trim();
                    sbJSON.Append(SafeJSON(s));
                }
                else
                {
                    XmlToJSONnode(sbJSON, (XmlElement)alChild, showNodeName);
                }
                sbJSON.Append(", ");
            }
            public static string SafeJSON(string s)
            {
                if ((s == null) || (s.Length == 0))
                {
                    return "\"\"";
                }
                int length = s.Length;
                StringBuilder builder = new StringBuilder(length + 4);
                builder.Append('"');
                for (int i = 0; i < length; i++)
                {
                    char ch = s[i];
                    switch (ch)
                    {
                        case '\\':
                        case '"':
                        case '>':
                            builder.Append('\\');
                            builder.Append(ch);
                            break;
                        case '\b':
                            builder.Append(@"\b");
                            break;
                        case '\t':
                            builder.Append(@"\t");
                            break;
                        case '\n':
                            builder.Append(@"\n");
                            break;
                        case '\f':
                            builder.Append(@"\f");
                            break;
                        case '\r':
                            builder.Append(@"\r");
                            break;
                        default:
                            if (ch < ' ')
                            {
                                string str2 = new string(ch, 1);
                                string str = "000" + int.Parse(str2, NumberStyles.HexNumber);
                                builder.Append(@"\u" + str.Substring(str.Length - 4));
                            }
                            else
                            {
                                builder.Append(ch);
                            }
                            break;
                    }
                }
                builder.Append('"');
                return builder.ToString();
            }
            private static void StoreChildNode(IDictionary childNodeNames, string nodeName, object nodeValue)
            {
                ArrayList list2;
                if (nodeValue is XmlElement)
                {
                    XmlNode node = (XmlNode)nodeValue;
                    if (node.Attributes.Count == 0)
                    {
                        XmlNodeList childNodes = node.ChildNodes;
                        if (childNodes.Count == 0)
                        {
                            nodeValue = null;
                        }
                        else if ((childNodes.Count == 1) && (childNodes[0] is XmlText))
                        {
                            nodeValue = childNodes[0].InnerText;
                        }
                    }
                }
                object obj2 = childNodeNames[nodeName];
                if (obj2 == null)
                {
                    list2 = new ArrayList();
                    childNodeNames[nodeName] = list2;
                }
                else
                {
                    list2 = (ArrayList)obj2;
                }
                list2.Add(nodeValue);
            }
            public static string XmlToJSON(XmlDocument xmlDoc)
            {
                StringBuilder sbJSON = new StringBuilder();
                sbJSON.Append("{ ");
                XmlToJSONnode(sbJSON, xmlDoc.DocumentElement, true);
                sbJSON.Append("}");
                return sbJSON.ToString();
            }
            private static void XmlToJSONnode(StringBuilder sbJSON, XmlNode node, bool showNodeName)
            {
                if (showNodeName)
                {
                    sbJSON.Append(node.Name + ": ");
                }
                sbJSON.Append("{");
                SortedList childNodeNames = new SortedList();
                if (node.Attributes != null)
                {
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        StoreChildNode(childNodeNames, attribute.Name, attribute.InnerText);
                    }
                }
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    if (node2 is XmlText)
                    {
                        StoreChildNode(childNodeNames, "value", node2.InnerText);
                    }
                    else if (node2 is XmlElement)
                    {
                        StoreChildNode(childNodeNames, node2.Name, node2);
                    }
                }
                foreach (string str in childNodeNames.Keys)
                {
                    ArrayList list2 = (ArrayList)childNodeNames[str];
                    if (list2.Count == 1)
                    {
                        OutputNode(str, list2[0], sbJSON, true);
                    }
                    else
                    {
                        sbJSON.Append(str + ": [ ");
                        foreach (object obj2 in list2)
                        {
                            OutputNode(str, obj2, sbJSON, false);
                        }
                        sbJSON.Remove(sbJSON.Length - 2, 2);
                        sbJSON.Append(" ], ");
                    }
                }
                sbJSON.Remove(sbJSON.Length - 2, 2);
                sbJSON.Append(" }");
            }
        }
    }这个可以否?
      

  5.   

    先JSON反序列化,再序列化成XML