C# 实体通过反射获取属性,动态生成XML格式

        /// <summary>
        ///  自助机格式xml 转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        public string XmlSerializeCallFunDitits<T>(T model)
        {
            try
            {
                var newType = model.GetType();
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                List<Dictionary<string, object>> dictionaryList = new List<Dictionary<string, object>>();
                #region  反射 dictionary 获取实体的字段     dictionaryList 获取集合实体中集合的字段
                foreach (var item in newType.GetRuntimeProperties())
                {
                    var type = item.PropertyType.Name;
                    var IsGenericType = item.PropertyType.IsGenericType;
                    var list = item.PropertyType.GetInterface("IEnumerable", false);
                    //Console.WriteLine($"属性名称:{item.Name},类型:{type},值:{item.GetValue(model)}");
                    if (IsGenericType && list != null)
                    {
                        var listVal = item.GetValue(model) as IEnumerable<object>;
                        if (listVal == null) continue;
                        foreach (var aa in listVal)
                        {
                            var dtype = aa.GetType();
                            var ret222 = new Dictionary<string, object>();
                            foreach (var bb in dtype.GetProperties())
                            {
                                var dtlName = bb.Name.ToLower();
                                var dtlType = bb.PropertyType.Name;
                                var oldValue = bb.GetValue(aa);
                                if (IsXmlSelfServicePrinting(dtlName))
                                    ret222.Add(dtlName, oldValue);
                                // Console.WriteLine($"子级属性名称:{dtlName},类型:{dtlType},值:{oldValue}");
                            }
                            dictionaryList.Add(ret222);
                        }
                    }
                    else
                    {
                        if (IsXmlSelfServicePrinting(item.Name))
                            dictionary.Add(item.Name, item.GetValue(model));
                    }
                }
                #endregion
                StringBuilder sb = new StringBuilder();
                string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> ";
                sb.Append(xml);
                sb.Append(string.Format("<Response>"));

                if (dictionary.Count() > 0)
                {
                    foreach (var itemDictionaryInfo in dictionary)
                    {
                        sb.Append(string.Format("<{0}>{1}</{2}>", itemDictionaryInfo.Key, itemDictionaryInfo.Value, itemDictionaryInfo.Key));
                    }
                }
                if (dictionaryList.Count() > 0)
                {
                    sb.Append(string.Format("<resultlist size=\"{0}\">", dictionaryList.Count()));
                    foreach (var itemDictionaryInfoList in dictionaryList)
                    {
                        sb.Append(string.Format("<result>"));
                        foreach (var itemDictionaryInfo in itemDictionaryInfoList)
                        {
                            sb.Append(string.Format("<{0}>{1}</{2}>", itemDictionaryInfo.Key, itemDictionaryInfo.Value, itemDictionaryInfo.Key));
                        }
                        sb.Append(string.Format("</result>"));
                    }
                    sb.Append(string.Format("</resultlist>"));
                }
                sb.Append(string.Format("</Response>"));
                return sb.ToString();
            }
            catch (Exception ex)
            {
                return "将实体对象转换成XML异常" + ex;
                throw new Exception("将实体对象转换成XML异常", ex);
            }
        }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值