List对象转自定义xml字符串
public static string CreateXmlListFile<T>(List<T> OBJ, string InterfaceName, string code, string message)
{
XmlDocument xmlDoc = new XmlDocument();
//创建类型声明节点
XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmlDoc.AppendChild(node);
//创建根节点
XmlNode root1 = xmlDoc.CreateElement("data");
XmlNode root2 = xmlDoc.CreateElement("message");
XmlElement re1 = (XmlElement)root1;
re1.SetAttribute("name", InterfaceName);//提交的接口名
re1.SetAttribute("code", code);//返回的状态码,0为失败,1为成功
XmlElement re2 = (XmlElement)root2;
re2.SetAttribute("message", message);//响应消息的内容
xmlDoc.AppendChild(root1);
root1.AppendChild(root2);
if (OBJ.Count > 0)
{
for (int i = 0; i < OBJ.Count; i++)
{
if (OBJ[i] != null)
{
XmlNode root3 = xmlDoc.CreateElement("record");
XmlElement re3 = (XmlElement)root3;
Type type = OBJ[i].GetType();
foreach (System.Reflection.PropertyInfo p in type.GetProperties())
{
string o = GetPropertyValue(OBJ[i], p.Name) == null ? "" : GetPropertyValue(OBJ[i], p.Name).ToString();
re3.SetAttribute(p.Name, o);
}
root1.AppendChild(root3);
}
}
}
return xmlDoc.InnerXml.ToString();
}
/// <summary>
/// 获取某个对象中的属性值
/// </summary>
/// <param name="info"></param>
/// <param name="field"></param>
/// <returns></returns>
public static object GetPropertyValue(object info, string field)
{
if (info == null) return null;
Type t = info.GetType();
IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
return property.First().GetValue(info, null);
}
**可生成xml,带有多个record节点,一个记录作为一个record节点。
xml转List对象
public static List<T> AnalysisXmlToList<T>(string xml) where T : class, new()
{
List<T> tList = new List<T>();
// 获得此模型的公共属性
// PropertyInfo[] propertys = t.GetType().GetProperties();
List<KValue> list = new List<KValue>();
var doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList nodelist = doc.SelectNodes("//record");
foreach (var item in nodelist)
{
T t = new T();//实例化对象
XmlNode node = (XmlNode)item;//将nodelist中的node取出
for (int i = 0; i < node.Attributes.Count; i++)
{
var s = node.Attributes;//取node的属性
KValue kv = new KValue()
{
key = node.Attributes[i].Name,
value = node.Attributes[i].Value == "" ? null : node.Attributes[i].Value
};
list.Add(kv);
}
for (int i = 0; i < list.Count; i++)
{
//根据属性名获得指定的属性对象
PropertyInfo gc = t.GetType().GetProperty(list[i].key);
gc.SetValue(t, Convert.ChangeType(list[i].value, gc.PropertyType), null);
}
tList.Add(t);
}
return tList;
}
public class KValue
{
public string key { get; set; }
public string value { get; set; }
}
**将自定义的xml字符串转List对象,where T : class, new()是泛型方法中实例化必须的。
Convert.ChangeType(list[i].value, gc.PropertyType),将数据更改为指定类型。
DataTable转换成List对象
/// <summary>
/// 利用反射将DataTable转换为List<T>对象
/// </summary>
/// <param name="dt">DataTable 对象</param>
/// <returns>List<T>集合</returns>
public static List<T> DataTableToList<T>(DataTable dt) where T : class, new()
{
// 定义集合
List<T> ts = new List<T>();
//定义一个临时变量
string tempName = string.Empty;
//遍历DataTable中所有的数据行
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
//遍历该对象的所有属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;//将属性名称赋值给临时变量
//检查DataTable是否包含此列(列名==对象的属性名)
if (dt.Columns.Contains(tempName))
{
//取值
object value = dr[tempName];
var values = dr[tempName].GetType();
//如果非空,则赋给对象的属性
if (value != DBNull.Value)
{
if (values.Name != pi.PropertyType.Name)
{
pi.SetValue(t, Convert.ChangeType(dr[pi.Name], pi.PropertyType), null);
}
else
{
pi.SetValue(t, value, null);
}
}
}
}
//对象添加到泛型集合中
ts.Add(t);
}
}
return ts;
}