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

泛型对象Lists转xml

上一篇:wpf 依赖属性实现原理(一种特殊的依赖属性)


下一篇:VS下遇到未能加载文件或程序集 错误