C# xml转换为对象

2021-01-09 04:28

阅读:725

标签:set   var   info   反序   cep   ons   new   res   root   

xfs1011001110124060005668136王志奇gfz1更新订单请使用更新接口

 

C#类

    [XmlRoot("responses")]
    public class Responses
    {
        //[XmlElement("response")]
        public YUN_DA_Response response { get; set; }

        public Responses()
        {
            response = new YUN_DA_Response();
        }
    }

    public class YUN_DA_Response
    {
        public string order_serial_no { get; set; }
        public string mail_no { get; set; }
        public string status { get; set; }
        public string msg { get; set; }

        public string pdf_info { get; set; }
    }

 

转换:

 /// 
        /// 反序列化
        /// 
        /// 实体
        /// xml文件
        /// 
        public T Deserialize(string strXml) where T : class
        {
            try
            {

                object obj;
                using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(strXml)))
                {
                    using (XmlReader xmlReader = XmlReader.Create(memoryStream))
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                        obj = xmlSerializer.Deserialize(xmlReader);
                    }
                }
                return obj as T;
            } 
            catch (Exception ex)
            {
                return null;
            }
        }

 

            string testStr = @"
xfs1011001110124060005668136王志奇gfz1更新订单请使用更新接口";

            var _t = Deserialize(testStr);

 

C# xml转换为对象

标签:set   var   info   反序   cep   ons   new   res   root   

原文地址:https://www.cnblogs.com/GarsonZhang/p/13112239.html


评论


亲,登录后才可以留言!