• 实现实体类和Xml相互转化


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Xml;
      6 
      7  public class XMLHelperToList<T> where T : new()
      8     {
      9         #region 实体类转成Xml
     10         /// <summary>
     11         /// 对象实例转成xml
     12         /// </summary>
     13         /// <param name="item">对象实例</param>
     14         /// <returns></returns>
     15         public static string EntityToXml(T item)
     16         {
     17             IList<T> items = new List<T>();
     18             items.Add(item);
     19             return EntityToXml(items);
     20         }
     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 
     41             return doc.InnerXml;
     42         }
     43 
     44         private static void EntityToXml(XmlDocument doc, XmlElement root, T item)
     45         {
     46             //创建元素
     47             XmlElement xmlItem = doc.CreateElement(typeof(T).Name);
     48             //对象的属性集
     49             System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
     50 
     51             foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
     52             {
     53                 if (pinfo != null)
     54                 {
     55                     //对象属性名称
     56                     string name = pinfo.Name;
     57                     //对象属性值
     58                     string value = String.Empty;
     59 
     60                     if (pinfo.GetValue(item, null) != null)
     61                         value = pinfo.GetValue(item, null).ToString();//获取对象属性值
     62                     //设置元素的属性值
     63                     xmlItem.SetAttribute(name, value);
     64                 }
     65             }
     66             //向根添加子元素
     67             root.AppendChild(xmlItem);
     68         }
     69 
     70 
     71         #endregion
     72 
     73         #region Xml转成实体类
     74 
     75         /// <summary>
     76         /// Xml转成对象实例
     77         /// </summary>
     78         /// <param name="xml">xml</param>
     79         /// <returns></returns>
     80         public static T XmlToEntity(string xml)
     81         {
     82             IList<T> items = XmlToEntityList(xml);
     83             if (items != null && items.Count > 0)
     84                 return items[0];
     85             else return default(T);
     86         }
     87 
     88         /// <summary>
     89         /// Xml转成对象实例集
     90         /// </summary>
     91         /// <param name="xml">xml</param>
     92         /// <returns></returns>
     93         public static IList<T> XmlToEntityList(string xml)
     94         {
     95             XmlDocument doc = new XmlDocument();
     96             try
     97             {
     98                 doc.LoadXml(xml);
     99             }
    100             catch
    101             {
    102                 return null;
    103             }
    104             if (doc.ChildNodes.Count != 1)
    105                 return null;
    106             if (doc.ChildNodes[0].Name.ToLower() != typeof(T).Name.ToLower() + "s")
    107                 return null;
    108 
    109             XmlNode node = doc.ChildNodes[0];
    110 
    111             IList<T> items = new List<T>();
    112 
    113             foreach (XmlNode child in node.ChildNodes)
    114             {
    115                 if (child.Name.ToLower() == typeof(T).Name.ToLower())
    116                     items.Add(XmlNodeToEntity(child));
    117             }
    118 
    119             return items;
    120         }
    121 
    122         private static T XmlNodeToEntity(XmlNode node)
    123         {
    124             T item = new T();
    125 
    126             if (node.NodeType == XmlNodeType.Element)
    127             {
    128                 XmlElement element = (XmlElement)node;
    129 
    130                 System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    131 
    132                 foreach (XmlAttribute attr in element.Attributes)
    133                 {
    134                     string attrName = attr.Name.ToLower();
    135                     string attrValue = attr.Value.ToString();
    136                     foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
    137                     {
    138                         if (pinfo != null)
    139                         {
    140                             string name = pinfo.Name.ToLower();
    141                             Type dbType = pinfo.PropertyType;
    142                             if (name == attrName)
    143                             {
    144                                 if (String.IsNullOrEmpty(attrValue))
    145                                     continue;
    146                                 switch (dbType.ToString())
    147                                 {
    148                                     case "System.Int32":
    149                                         pinfo.SetValue(item, Convert.ToInt32(attrValue), null);
    150                                         break;
    151                                     case "System.Boolean":
    152                                         pinfo.SetValue(item, Convert.ToBoolean(attrValue), null);
    153                                         break;
    154                                     case "System.DateTime":
    155                                         pinfo.SetValue(item, Convert.ToDateTime(attrValue), null);
    156                                         break;
    157                                     case "System.Decimal":
    158                                         pinfo.SetValue(item, Convert.ToDecimal(attrValue), null);
    159                                         break;
    160                                     case "System.Double":
    161                                         pinfo.SetValue(item, Convert.ToDouble(attrValue), null);
    162                                         break;
    163                                     default:
    164                                         pinfo.SetValue(item, attrValue, null);
    165                                         break;
    166                                 }
    167                                 continue;
    168                             }
    169                         }
    170                     }
    171                 }
    172             }
    173             return item;
    174         }
    175         #endregion
    176     }
    实体类和Xml相互转化
  • 相关阅读:
    类加载
    LinkedList插入排序实现
    99乘法表
    关于IO流的抽象类
    分解质因数
    Struts2小demo遇到的几个问题
    Tomcat设置欢迎页问题
    数据库迁移
    EF – 1.模式
    正则表达式
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/4672097.html
Copyright © 2020-2023  润新知