using System;
using System.Collections.Generic;
using System.Xml;
namespace Common
{
public class XmlHelper
{
///
/// 将XML内容转换成目标对象实体集合
///
/// 目标对象实体
/// 完整文件名(根目录下只需文件名称)
///
///
public static List ConvertXMLToObject(string FileName, string WrapperNodeName = "UrlSetting")
{
XmlDocument doc = new XmlDocument();
doc.Load(FileName);
List result = new List();
var TType = typeof(T);
XmlNodeList nodeList = doc.ChildNodes;
if (!string.IsNullOrEmpty(WrapperNodeName))
{
foreach (XmlNode node in doc.ChildNodes)
{
if (node.Name == WrapperNodeName)
{
nodeList = node.ChildNodes;
break;
}
}
}
object oneT = null;
foreach (XmlNode node in nodeList)
{
if (node.NodeType == XmlNodeType.Comment || node.NodeType == XmlNodeType.XmlDeclaration) continue;
oneT = TType.Assembly.CreateInstance(TType.FullName);
foreach (XmlNode item in node.ChildNodes)
{
if (item.NodeType == XmlNodeType.Comment) continue;
var property = TType.GetProperty(item.Name);
if (property != null)
property.SetValue(oneT, Convert.ChangeType(item.InnerText, property.PropertyType), null);
}
result.Add((T)oneT);
}
return result;
}
/////
///// 从作业数据地图中获取配置信息
/////
///// 作业数据地图
/////
//public static FCSConfig GetConfigFromDataMap(JobDataMap datamap)
//{
// FCSConfig config = new FCSConfig();
// var properties = typeof(FCSConfig).GetProperties();
// foreach (PropertyInfo info in properties)
// {
// if (info.PropertyType == typeof(string))
// info.SetValue(config, datamap.GetString(info.Name), null);
// else if (info.PropertyType == typeof(Int32))
// info.SetValue(config, datamap.GetInt(info.Name), null);
// }
// return config;
//}
}
}