none
序列化 XML 到 JSON 再轉成 Class 問題 RRS feed

  • 問題

  • 使用 XML 轉成 JSON 再轉成 Class 時,其中一的屬性無法轉成功,請大家幫我查一下原因。

    JSON class

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication7
    {
        [DataContract]
        public class Rootobject
        {
            [DataMember(Name = "message")]
            public Message message { get; set; }
        }
    
        [DataContract]
        public class Message
        {
            [JsonProperty("id")]
            [DataMember(Name = "@id")]
            public string id { get; set; }
    
            [JsonProperty("from")]
            [DataMember(Name = "@from")]
            public string from { get; set; }
    
            [JsonProperty("to")]
            [DataMember(Name = "@to")]
            public string to { get; set; }
    
            [JsonProperty("type")]
            [DataMember(Name = "@type")]
            public string type { get; set; }
    
            [DataMember(Name = "body")]
            public string body { get; set; }
    
            [DataMember(Name = "multimedia")]
            public Multimedia multimedia { get; set; }
        }
    
        [DataContract(Name = "multimedia")]
        public class Multimedia
        {
            [JsonProperty("xmlns")]
            [DataMember(Name = "@xmlns")]
            public string xmlns { get; set; }
    
            [DataMember(Name = "param")]
            public List<Param> param { get; set; }
        }
    
        [DataContract(Name = "param")]
        public class Param
        {
            [JsonProperty("name")]
            [DataMember(Name = "@name")]
            public string name { get; set; }
    
            [JsonProperty("text")]
            [DataMember(Name = "#text")]
            public string text { get; set; }
        }
    }

    主程式

    using ConsoleApplication7;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string msg = "<message to=\"demo.papa@xxx.xxx.xxx.xxx\" id=\"2015-08-20-11:09:56.446\" type=\"chat\" from=\"demo.teacher@xxx.xxx.xxx.xxx/asus-ASUS_T00P-1440040178697\"><body>356</body><multimedia xmlns=\"showit:xmpp:multimedia_message\"><param name=\"type\">1</param></multimedia></message>";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(msg);
                string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
    
                Rootobject people = null;
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Rootobject));
                    people = (Rootobject)serializer.ReadObject(ms);
                }
    
                Console.ReadLine();
            }
    
    
        }
    }

    XML 格式

    <message to="demo.papa@xxx.xxx.xxx.xxx" id="2015-08-20-11:09:56.446" type="chat" from="demo.teacher@xxx.xxx.xxx.xxx/asus-ASUS_T00P-1440040178697">
    	<body>356</body>
    	<multimedia xmlns="showit:xmpp:multimedia_message">
    		<param name="type">1</param>
    	</multimedia>
    </message>


    • 已編輯 Mr. Kao 2015年8月21日 上午 02:26 補充內容
    2015年8月21日 上午 02:25

解答

  • param 不要 List 試試看
     [DataContract(Name = "multimedia")]
        public class Multimedia
        {
            [JsonProperty("xmlns")]
            [DataMember(Name = "@xmlns")]
            public string xmlns { get; set; }
    
            [DataMember(Name = "param")]
            public Param param { get; set; }
        }

    2015年8月21日 上午 03:28

所有回覆