泛型对象Lists转xml

时间:2014-01-16 08:56:35   收藏:0   阅读:353

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

原文:http://www.cnblogs.com/xiguanjiandan/p/3517827.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!