泛型对象Lists转xml

public static string ListtoXML<T>(List<T> lists)
{
if (lists.Count <= 0)
{
return "<data><row/><data/>";
}
StringBuilder xml = new StringBuilder();

T dt = lists[0];
System.Reflection.PropertyInfo[] pis = dt.GetType().GetProperties();
int count = pis.Length;

xml.Append("<data>");
foreach (T t in lists)
{
System.Reflection.PropertyInfo[] ts = t.GetType().GetProperties();
xml.Append("<row ");
for (int i = 0; i < count; i++)
{
xml.Append(ts[i].Name + "='" + ts[i].GetValue(t, null).ToString().Trim() + "' ");
}
xml.Append("/>");
}

xml.Append("<data/>");
return xml.ToString();
}

原文地址:https://www.cnblogs.com/xiguanjiandan/p/3517827.html