XMLHelperToList.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. namespace Ant.Service.Common.ToolsHelper
  7. {
  8. public class XMLHelperToList<T> where T : new()
  9. {
  10. #region 实体类转成Xml
  11. /// <summary>
  12. /// 对象实例转成xml
  13. /// </summary>
  14. /// <param name="item">对象实例</param>
  15. /// <returns></returns>
  16. public static string EntityToXml(T item)
  17. {
  18. IList<T> items = new List<T>();
  19. items.Add(item);
  20. return EntityToXml(items);
  21. }
  22. /// <summary>
  23. /// 对象实例集转成xml
  24. /// </summary>
  25. /// <param name="items">对象实例集</param>
  26. /// <returns></returns>
  27. public static string EntityToXml(IList<T> items)
  28. {
  29. //创建XmlDocument文档
  30. XmlDocument doc = new XmlDocument();
  31. //创建根元素
  32. XmlElement root = doc.CreateElement(typeof(T).Name + "s");
  33. //添加根元素的子元素集
  34. foreach (T item in items)
  35. {
  36. EntityToXml(doc, root, item);
  37. }
  38. //向XmlDocument文档添加根元素
  39. doc.AppendChild(root);
  40. return doc.InnerXml;
  41. }
  42. private static void EntityToXml(XmlDocument doc, XmlElement root, T item)
  43. {
  44. //创建元素
  45. XmlElement xmlItem = doc.CreateElement(typeof(T).Name);
  46. //对象的属性集
  47. System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  48. foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
  49. {
  50. if (pinfo != null)
  51. {
  52. //对象属性名称
  53. string name = pinfo.Name;
  54. //对象属性值
  55. string value = String.Empty;
  56. if (pinfo.GetValue(item, null) != null)
  57. value = pinfo.GetValue(item, null).ToString();//获取对象属性值
  58. //设置元素的属性值
  59. xmlItem.SetAttribute(name, value);
  60. }
  61. }
  62. //向根添加子元素
  63. root.AppendChild(xmlItem);
  64. }
  65. #endregion
  66. #region Xml转成实体类
  67. /// <summary>
  68. /// Xml转成对象实例
  69. /// </summary>
  70. /// <param name="xml">xml</param>
  71. /// <returns></returns>
  72. public static T XmlToEntity(string xml)
  73. {
  74. IList<T> items = XmlToEntityList(xml);
  75. if (items != null && items.Count > 0)
  76. return items[0];
  77. else return default(T);
  78. }
  79. /// <summary>
  80. /// Xml转成对象实例集
  81. /// </summary>
  82. /// <param name="xml">xml</param>
  83. /// <returns></returns>
  84. public static IList<T> XmlToEntityList(string xml)
  85. {
  86. XmlDocument doc = new XmlDocument();
  87. try
  88. {
  89. doc.LoadXml(xml);
  90. }
  91. catch
  92. {
  93. return null;
  94. }
  95. if (doc.ChildNodes.Count != 1)
  96. return null;
  97. if (doc.ChildNodes[0].Name.ToLower() != typeof(T).Name.ToLower() + "s")
  98. return null;
  99. XmlNode node = doc.ChildNodes[0];
  100. IList<T> items = new List<T>();
  101. foreach (XmlNode child in node.ChildNodes)
  102. {
  103. if (child.Name.ToLower() == typeof(T).Name.ToLower())
  104. items.Add(XmlNodeToEntity(child));
  105. }
  106. return items;
  107. }
  108. private static T XmlNodeToEntity(XmlNode node)
  109. {
  110. T item = new T();
  111. if (node.NodeType == XmlNodeType.Element)
  112. {
  113. XmlElement element = (XmlElement)node;
  114. System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  115. foreach (XmlAttribute attr in element.Attributes)
  116. {
  117. string attrName = attr.Name.ToLower();
  118. string attrValue = attr.Value.ToString();
  119. foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
  120. {
  121. if (pinfo != null)
  122. {
  123. string name = pinfo.Name.ToLower();
  124. Type dbType = pinfo.PropertyType;
  125. if (name == attrName)
  126. {
  127. if (String.IsNullOrEmpty(attrValue))
  128. continue;
  129. switch (dbType.ToString())
  130. {
  131. case "System.Int32":
  132. pinfo.SetValue(item, Convert.ToInt32(attrValue), null);
  133. break;
  134. case "System.Boolean":
  135. pinfo.SetValue(item, Convert.ToBoolean(attrValue), null);
  136. break;
  137. case "System.DateTime":
  138. pinfo.SetValue(item, Convert.ToDateTime(attrValue), null);
  139. break;
  140. case "System.Decimal":
  141. pinfo.SetValue(item, Convert.ToDecimal(attrValue), null);
  142. break;
  143. case "System.Double":
  144. pinfo.SetValue(item, Convert.ToDouble(attrValue), null);
  145. break;
  146. default:
  147. pinfo.SetValue(item, attrValue, null);
  148. break;
  149. }
  150. continue;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. return item;
  157. }
  158. #endregion
  159. }
  160. }