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