现在的位置: 首页 > 综合 > 正文

IList与Xml互相转换(转载)

2012年11月01日 ⁄ 综合 ⁄ 共 7029字 ⁄ 字号 评论关闭

原文地址:http://www.cnblogs.com/yknb/archive/2009/03/06/1404986.html

我们在进行数据传递时很多用到xml数据格式。数据接收后对数据进行编程操作时,面向对象的方式更容易。

这就需要我们在对象与xml间进行相互的转换。自己写了一个类基本实现了上述功能。

 


  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 
  8 namespace YKSoft.BLL.Utility
  9 {
 10     /// <summary>
 11     /// 实体转Xml,Xml转实体类
 12     /// </summary>
 13     /// <typeparam name="T"></typeparam>
 14     public class XmlHelper<T> where T : new()
 15     {
 16         #region 实体类转成Xml
 17         /// <summary>
 18         /// 对象实例转成xml
 19         /// </summary>
 20         /// <param name="item">对象实例</param>
 21         /// <returns></returns>
 22         public static string EntityToXml(T item)
 23         {
 24             IList<T> items = new List<T>();
 25             items.Add(item);
 26             return EntityToXml(items);
 27         }
 28 
 29         /// <summary>
 30         /// 对象实例集转成xml
 31         /// </summary>
 32         /// <param name="items">对象实例集</param>
 33         /// <returns></returns>
 34         public static string EntityToXml(IList<T> items)
 35         {
 36             //创建XmlDocument文档
 37             XmlDocument doc = new XmlDocument();
 38             //创建根元素
 39             XmlElement root = doc.CreateElement(typeof(T).Name + "s");
 40             //添加根元素的子元素集
 41             foreach (T item in items)
 42             {
 43                 EntityToXml(doc, root, item);
 44             }
 45             //向XmlDocument文档添加根元素
 46             doc.AppendChild(root);
 47             
 48             return doc.InnerXml;
 49         }
 50 
 51         private static void EntityToXml(XmlDocument doc, XmlElement root, T item)
 52         {
 53             //创建元素
 54             XmlElement xmlItem = doc.CreateElement(typeof(T).Name);
 55             //对象的属性集
 56            

System.Reflection.PropertyInfo[] propertyInfo =
typeof(T).GetProperties(System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance);

 


 57 
 58             foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
 59             {
 60                 if (pinfo != null)
 61                 {
 62                     //对象属性名称
 63                     string name = pinfo.Name;
 64                     //对象属性值
 65                     string value = String.Empty;
 66                     
 67                     if (pinfo.GetValue(item, null!= null)
 68                         value = pinfo.GetValue(item, null).ToString();//获取对象属性值
 69                     //设置元素的属性值
 70                     xmlItem.SetAttribute(name,value);
 71                 }
 72             }
 73             //向根添加子元素
 74             root.AppendChild(xmlItem);
 75         }
 76 
 77        
 78         #endregion
 79 
 80         #region Xml转成实体类
 81 
 82         /// <summary>
 83         /// Xml转成对象实例
 84         /// </summary>
 85         /// <param name="xml">xml</param>
 86         /// <returns></returns>
 87         public static T XmlToEntity(string xml)
 88         {
 89            IList<T> items= XmlToEntityList(xml);
 90            if (items != null && items.Count > 0)
 91                return items[0];
 92            else return default(T);
 93         }
 94 
 95         /// <summary>
 96         /// Xml转成对象实例集
 97         /// </summary>
 98         /// <param name="xml">xml</param>
 99         /// <returns></returns>
100         public static IList<T> XmlToEntityList(string xml)
101         {
102             XmlDocument doc = new XmlDocument();
103             try
104             {
105                 doc.LoadXml(xml);
106             }
107             catch
108             {
109                 return null;
110             }
111             if (doc.ChildNodes.Count != 1)
112                 return null;
113             if (doc.ChildNodes[0].Name.ToLower() != typeof(T).Name.ToLower() + "s")
114                 return null;
115 
116             XmlNode node = doc.ChildNodes[0];
117 
118             IList<T> items = new List<T>();
119 
120             foreach (XmlNode child in node.ChildNodes)
121             {
122                 if (child.Name.ToLower() == typeof(T).Name.ToLower())
123                     items.Add(XmlNodeToEntity(child));
124             }
125 
126             return items;
127         }
128 
129         private static T XmlNodeToEntity(XmlNode node) 
130         {
131             T item = new T();
132 
133             if (node.NodeType == XmlNodeType.Element)
134             {
135                 XmlElement element = (XmlElement)node;
136 
137                 System.Reflection.PropertyInfo[] propertyInfo =
typeof
(T).GetProperties(System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance);
138 
139                 foreach (XmlAttribute attr in element.Attributes)
140                 {
141                     string attrName = attr.Name.ToLower();
142                     string attrValue = attr.Value.ToString();
143                     foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
144                     {
145                         if (pinfo != null)
146                         {
147                             string name = pinfo.Name.ToLower();
148                             Type dbType = pinfo.PropertyType;
149                             if (name == attrName)
150                             {
151                                 if (String.IsNullOrEmpty(attrValue))
152                                     continue;
153                                 switch (dbType.ToString())
154                                 {
155                                     case "System.Int32":
156                                         pinfo.SetValue(item, Convert.ToInt32(attrValue), null);
157                                         break;
158                                     case "System.Boolean":
159                                         pinfo.SetValue(item, Convert.ToBoolean(attrValue), null);
160                                         break;
161                                     case "System.DateTime"
162                                         pinfo.SetValue(item, Convert.ToDateTime(attrValue), null);
163                                         break;
164                                     case "System.Decimal":
165                                         pinfo.SetValue(item, Convert.ToDecimal(attrValue), null);
166                                         break;
167                                     case "System.Double":
168                                         pinfo.SetValue(item, Convert.ToDouble(attrValue), null);
169                                         break;
170                                     default:
171                                         pinfo.SetValue(item, attrValue, null);
172                                         break;
173                                 }
174                                 continue;
175                             }
176                         }
177                     }
178                 }
179             }
180             return item;
181         }
182 
183         #endregion
184     }
185        
186 }
187 

抱歉!评论已关闭.