泛型对象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();
}

posted on 2014-01-13 17:51  习惯简单  阅读(421)  评论(0编辑  收藏  举报

导航