zoukankan      html  css  js  c++  java
  • 实体 和 XML格式的转换

    许多接口传输需要XML格式转换,那么如何实现?看下面帮助类→_→

      /// <summary>
        /// XML处理帮助类,编码格式GBK!!
        /// </summary>
        public class XmlUtility
        {
            /// <summary>
            /// 将自定义对象序列化为XML字符串
            /// </summary>
            /// <param name="myObject">自定义对象实体</param>
            /// <returns>序列化后的XML字符串</returns>
            public static string SerializeToXml<T>(T obj)
            {
                try
                {
                    MemoryStream ms = new MemoryStream();
                    StreamWriter sw = new StreamWriter(ms, Encoding.GetEncoding("GBK"));
                    var ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    Type t = obj.GetType();
                    XmlSerializer serializer = new XmlSerializer(obj.GetType());
                    serializer.Serialize(sw, obj, ns);
    
                    string xmlMessage = Encoding.GetEncoding("GBK").GetString(ms.GetBuffer());
                    sw.Close();
                    ms.Close();
                    return xmlMessage.ToString();
                }
                catch (Exception ex)
                {
                    throw new Exception("将实体对象转换成XML异常", ex);
                }
            }
    
            /// <summary>
            /// 将XML字符串反序列化为对象
            /// </summary>
            /// <typeparam name="T">对象类型</typeparam>
            /// <param name="xml">XML字符</param>
            /// <returns></returns>
            public static T DeserializeToObject<T>(string xml) where T : class
            {
                try
                {
                    using (StringReader sr = new StringReader(xml))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(T));
                        return serializer.Deserialize(sr) as T;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("将XML转换成实体对象异常", ex);
                }
            }
        }

    根据上面类我简单写了一个Demo:

        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                //获取参数
                RequestModel r = new RequestModel();
                UpdateModel<RequestModel>(r);
                //拼接参数展示文字
                string msg = r.Name + "今年" + r.Age + "岁了";
                return Content(msg);
            }
        
            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
        }

  • 相关阅读:
    net.sf.json.JSONException: There is a cycle in the hierarchy!
    数据源的配置
    java枚举使用详解
    hibernate级联保存,更新个人遇到的问题
    NonUniqueObjectException 问题
    No.2 dotnetcore&docker--数据库等在Docker上的配置
    No.1 dotnetcore&docker--环境搭建
    No.5 dotnetcore&docker--采用Ambry做文件服务器
    No.3 dotnetcore&docker--搭建一个nuget服务器
    关于APM数据采集实例以及Eureka整合的一个想法
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/10963734.html
Copyright © 2011-2022 走看看