123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Generic;
- using System.Xml;
- namespace Common
- {
- public class XmlHelper
- {
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
|