C# 通过反射 Model 转 XML

Order.OrderRequest m = new Order.OrderRequest();
            m.Request = new Order.OrderRequest.RequestModel();
            m.Request.service = "123";
            m.Request.lang = "zzz";
            m.Head = "ssss";
            m.Body = new Order.OrderRequest.BodyModel();
            m.Body.Order = new Order.OrderRequest.BodyModel.OrderModel();
            m.Body.Order.orderid = "232323";
            m.Cargo = new List<Order.OrderRequest.CargoModel>();
            m.Cargo.Add(new Order.OrderRequest.CargoModel() { nameDDD = "123123" });
            m.Cargo.Add(new Order.OrderRequest.CargoModel() { nameDDD = "123123" });

 

StringBuilder str = new StringBuilder();
            ModelRecurveToXML(m, str);

 

//方法

  public static StringBuilder ModelRecurveToXML<T>(T model, StringBuilder builder)
        {
            StringBuilder builder3 = new StringBuilder();

            foreach (System.Reflection.PropertyInfo pro in model.GetType().GetProperties())
            {
                if (pro.PropertyType.IsPrimitive || pro.PropertyType.IsSealed)
                {
                    builder.Append(string.Format("<{0}>{1}<{2}>", pro.Name, pro.GetValue(model, null), pro.Name));

                }
                else
                {
                    var IsGenericType = pro.PropertyType.IsGenericType;
                    var list = pro.PropertyType.GetInterface("IEnumerable", false);
                    if (IsGenericType && list != null)
                    {
                        builder.Append(string.Format("<{0}", pro.Name));
                        var listVal = pro.GetValue(model, null) as IEnumerable<object>;
                        if (listVal == null) continue;
                        StringBuilder listBuilder = new StringBuilder();
                       
                        foreach (var aa in listVal)
                        {
                            var dtype = aa.GetType();
                            foreach (var bb in dtype.GetProperties())
                            {
                                listBuilder.Append(string.Format(" {0}={1}", bb.Name, bb.GetValue(aa, null)));
                            }
                        }
                        builder.Append(string.Format("{0}></{1}>", listBuilder, pro.Name));
                    }
                    else
                    {
                        builder3.Insert(0, string.Format("</{0}>", pro.Name));
                        builder.Append(string.Format("<{0}", pro.Name));
                        StringBuilder builder2 = new StringBuilder();
                        foreach (var item in pro.GetValue(model, null).GetType().GetProperties())
                        {

                            if (item.PropertyType.IsPrimitive || item.PropertyType.IsSealed)
                            {
                                builder2.Append(string.Format(" {0}={1}", item.Name, item.GetValue(pro.GetValue(model, null), null)));
                            }
                            else
                            {
                                builder.Append(string.Format(">"));
                                builder3.Insert(0, string.Format("</{0}>", item.Name));
                                builder.Append(string.Format("<{0}", item.Name));
                                var data2 = item.GetValue(pro.GetValue(model, null), null);
                                foreach (var item2 in data2.GetType().GetProperties())
                                {
                                    if (item2.PropertyType.IsPrimitive || item2.PropertyType.IsSealed)
                                    {
                                        builder2.Append(string.Format(" {0}={1}", item2.Name, item2.GetValue(data2, null)));
                                    }
                                    else
                                    {
                                        builder3.Insert(0, string.Format("</{0}>", item2.Name));
                                    }


                                }
                            }
                        }
                        builder.Append(string.Format("{0}>", builder2.ToString()));
                    }


                    
                }
            }
            builder.Append(builder3);
            return builder;
        }

//模型

 public class Order
        {

            public class OrderRequest
            {
                public RequestModel Request { get; set; }
                public class RequestModel
                {
                    public string service { get; set; }
                    public string lang { get; set; }
                }

                public string Head { get; set; }
                public BodyModel Body { get; set; }
                public class BodyModel
                {
                    public OrderModel Order { get; set; }
                    public class OrderModel
                    {
                        public string orderid { get; set; }
                    }
                }
                public List<CargoModel> Cargo { get; set; }
                public class CargoModel
                {
                    public string nameDDD { get; set; }
                }
            }
        }

 

生成

<Request service=123 lang=zzz><Head>ssss<Head><Body><Order orderid=232323><Cargo nameDDD=123123 nameDDD=123123></Cargo></Order></Body></Request>

 

posted @ 2021-03-06 10:43  LuoCore  阅读(227)  评论(0编辑  收藏  举报