123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Generic;
- using System.Xml;
- namespace Common
- {
- public class XmlHelper
- {
- /// <summary>
- /// 将XML内容转换成目标对象实体集合
- /// </summary>
- /// <typeparam name="T">目标对象实体</typeparam>
- /// <param name="FileName">完整文件名(根目录下只需文件名称)</param>
- /// <param name="WrapperNodeName"></param>
- /// <returns></returns>
- public static List<T> ConvertXMLToObject<T>(string FileName, string WrapperNodeName = "UrlSetting")
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(FileName);
- List<T> result = new List<T>();
- 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;
- }
- ///// <summary>
- ///// 从作业数据地图中获取配置信息
- ///// </summary>
- ///// <param name="datamap">作业数据地图</param>
- ///// <returns></returns>
- //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;
- //}
- }
- }
|