1 public class XmlParser
2 {
3 public static IList<T> Parse<T>(
string xmlContent)
where T :
new()
4 {
5 try 6 {
7 if (
string.IsNullOrWhiteSpace(xmlContent))
8 {
9 return new List<T>();
10 }
11 12 using (StringReader reader =
new StringReader(xmlContent))
13 {
14 return Parse<T>(XmlReader.Create(reader));
15 }
16 }
17 catch 18 {
19 return new List<T>();
20 }
21 }
22 23 public static IList<T> Parse<T>(XDocument document)
where T :
new()
24 {
25 if (document ==
null)
26 {
27 return new List<T>();
28 }
29 30 string className = AttributeUtility.GetClassName<T>();
31 IEnumerable<XElement> elements = document.Root.Elements(className);
32 33 return Parse<T>(elements);
34 }
35 36 public static IList<T> Parse<T>(IEnumerable<XElement> elements)
where T :
new()
37 {
38 if (elements ==
null || elements.Count() ==
0)
39 {
40 return new List<T>();
41 }
42 43 try 44 {
45 var propertyDic = ReflectionUtility.GetPropertyMapperDictionary<T>();
46 List<T> entities =
new List<T>();
47 IEnumerable<XElement> innerElements =
null;
48 T entity =
new T();
49 50 foreach (XElement element
in elements)
51 {
52 entity =
new T();
53 entities.Add(entity);
54 innerElements = element.Elements();
55 56 foreach (XElement innerElement
in innerElements)
57 {
58 SetPropertyValue<T>(propertyDic, entity, innerElement.Name.ToString(), innerElement.Value);
59 }
60 }
61 62 return entities;
63 }
64 catch 65 {
66 return new List<T>();
67 }
68 }
69 70 public static IList<T> Parse<T>(XmlReader xmlReader)
where T :
new()
71 {
72 try 73 {
74 if (xmlReader ==
null)
75 {
76 return new List<T>();
77 }
78 79 return ParseXmlReader<T>(xmlReader);
80 }
81 catch 82 {
83 return new List<T>();
84 }
85 }
86 87 private static IList<T> ParseXmlReader<T>(XmlReader xmlReader)
where T :
new()
88 {
89 List<PropertyInfo> properties =
new List<PropertyInfo>(
typeof(T).GetProperties());
90 var propertyDic = ReflectionUtility.GetPropertyMapperDictionary<T>();
91 string className = AttributeUtility.GetClassName<T>();
92 IList<T> entities =
new List<T>();
93 T entity =
new T();
94 string lastElementName =
null;
95 96 while (xmlReader.Read())
97 {
98 switch (xmlReader.NodeType)
99 {
100 case XmlNodeType.Element:
101 if (
string.Equals(xmlReader.Name, className))
102 {
103 entity =
new T();
104 entities.Add(entity);
105 }
106 lastElementName = xmlReader.Name;
107 break;
108 case XmlNodeType.Text:
109 SetPropertyValue<T>(propertyDic, entity, lastElementName, xmlReader.Value);
110 break;
111 default:
112 break;
113 }
114 }
115 116 return entities;
117 }
118 119 private static void SetPropertyValue<T>(Dictionary<
string, PropertyInfo> propertyDic, T entity,
string lastElementName,
string value)
where T :
new()
120 {
121 if (!
string.IsNullOrWhiteSpace(lastElementName) && propertyDic.ContainsKey(lastElementName))
122 {
123 PropertyInfo currentProperty = propertyDic[lastElementName];
124 if (currentProperty !=
null && currentProperty.CanWrite)
125 {
126 object invokeResult =
new FuncDictionary().DynamicInvoke(currentProperty.PropertyType, value);
127 currentProperty.SetValue(entity, invokeResult,
null);
128 }
129 }
130 }
131 }