zoukankan      html  css  js  c++  java
  • C# xml转换为对象

    <responses> 
      <response> 
        <order_serial_no>xfs101100111012</order_serial_no>  
        <mail_no>4060005668136</mail_no>  
        <pdf_info>王志奇gfz</pdf_info>  
        <status>1</status>  
        <msg>更新订单请使用更新接口</msg> 
      </response> 
    </responses>

    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; }
        }

    转换:

     /// <summary>
            /// 反序列化
            /// </summary>
            /// <typeparam name="T">实体</typeparam>
            /// <param name="strXml">xml文件</param>
            /// <returns></returns>
            public T Deserialize<T>(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 = @"
    <responses> 
      <response> 
        <order_serial_no>xfs101100111012</order_serial_no>  
        <mail_no>4060005668136</mail_no>  
        <pdf_info>王志奇gfz</pdf_info>  
        <status>1</status>  
        <msg>更新订单请使用更新接口</msg> 
      </response> 
    </responses>
    ";
    
                var _t = Deserialize<Responses>(testStr);
    慎于行,敏于思!GGGGGG
  • 相关阅读:
    LF will be replaced by CRLF in package.json.
    JS 防抖和节流
    webpack依赖分析|打包优化
    JavaScript 中精度问题以及解决方案
    mysql查询某些数据的最小值
    mongodb sort
    常用的快捷键(用到时再不断完善)
    一、negut增加Nancy,和Nancy.Hosting.Self,
    Linux系统(Centos)安装tomcat和部署Web项目
    CentOS 7系统安装jdk-8u161-linux-x64.tar.gz
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/13112239.html
Copyright © 2011-2022 走看看