xml

/// <summary>
/// 根据配置文件路径和根节点转换为对应的Model集合
/// 转换失败则返回空集合
/// </summary>
/// <typeparam name="T">Model</typeparam>
/// <param name="path">配置文件的完整路径</param>
/// <param name="parentElement">查找的父级节点</param>
/// <returns>Model集合</returns>
private static T GetModuleInfo<T>(string path, string parentElement)
{
var xml = XElement.Load(path);
var elements = from ele in xml.Elements(parentElement)
select ele;
var tObject = Activator.CreateInstance<T>();
try
{
foreach (var element in elements)
{
var property = tObject.GetType().GetProperties();
foreach (var item in property)
{
item.SetValue(tObject, element.Element(item.Name).Value, null);
}
break;
}
}
catch (Exception ex)
{
var message = "job配置信息转换成实体错误";
WriteExLog(ex, message, "GetModuleInfo<T>");
}
return tObject;
}

posted @ 2015-03-02 16:33  茂茂  阅读(138)  评论(0编辑  收藏  举报