原文地址:http://www.cnblogs.com/yknb/archive/2009/03/06/1404986.html
我们在进行数据传递时很多用到xml数据格式。数据接收后对数据进行编程操作时,面向对象的方式更容易。
这就需要我们在对象与xml间进行相互的转换。自己写了一个类基本实现了上述功能。
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