EntyToXml.cs 6.4 KB

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