using System; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Text.RegularExpressions; namespace DSWeb.SoftMng.XmlHelper { /// /// 实体转Xml,Xml转实体类 /// /// public class XmlHelper where T : new() { #region 实体类转成Xml /// /// 对象实例转成xml /// /// 对象实例 /// public static XmlElement EntityToXml(XmlDocument doc, T item, XmlElement root, ArrayList ignore = null) { return EntityToXml(doc,root,item, ignore); } /// /// 对象实例集转成xml /// /// 对象实例集 /// public static XmlElement EntityToXml(XmlDocument doc, IList items,string warp, ArrayList ignore = null) { //创建根元素 XmlElement root = doc.CreateElement(warp); //添加根元素的子元素集 foreach (T item in items) { EntityToXml(doc, root, item, ignore); } //向XmlDocument文档添加根元素 return root; } private static XmlElement EntityToXml(XmlDocument doc, XmlElement root, T item, ArrayList ignore) { //创建元素 XmlElement xmlItem = doc.CreateElement(typeof(T).Name); //对象的属性集 System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo) { if (pinfo != null) { //对象属性名称 string name = pinfo.Name; if (ignore != null) { if (ignore.Contains(name)) continue; } if (new Regex(@"\w+_Text|[G|P]ID").IsMatch(name)) continue; //对象属性值 string value = String.Empty; if (pinfo.GetValue(item, null) != null) value = pinfo.GetValue(item, null).ToString();//获取对象属性值 XmlElement elem = doc.CreateElement(name); if ((name == "IEDate" || name == "PDate" || name == "DDate" || name== "BillLadDate" || name== "ProdValidDt" || name== "DespDate" || name== "ValidTime") && !String.IsNullOrEmpty(value)) value = Convert.ToDateTime(value).ToString("yyyyMMdd"); //if(name == "ProduceDate" && !String.IsNullOrEmpty(value)) //value = Convert.ToDateTime(value).ToString("yyyy-MM-dd"); if (name == "SignTime" && !String.IsNullOrEmpty(value)) value = Convert.ToDateTime(value).ToString("yyyyMMdd HH:mm:ss"); if(!String.IsNullOrEmpty(value)) elem.InnerText = value; xmlItem.AppendChild(elem); } } //向根添加子元素 root.AppendChild(xmlItem); return xmlItem; } #endregion #region Xml转成实体类 /// /// Xml转成对象实例 /// /// xml /// public static T XmlToEntity(string xml) { IList items = XmlToEntityList(xml); if (items != null && items.Count > 0) return items[0]; else return default(T); } /// /// Xml转成对象实例集 /// /// xml /// public static IList XmlToEntityList(string xml) { XmlDocument doc = new XmlDocument(); try { doc.LoadXml(xml); } catch { return null; } if (doc.ChildNodes.Count != 1) return null; if (doc.ChildNodes[0].Name.ToLower() != typeof(T).Name.ToLower() + "s") return null; XmlNode node = doc.ChildNodes[0]; IList items = new List(); foreach (XmlNode child in node.ChildNodes) { if (child.Name.ToLower() == typeof(T).Name.ToLower()) items.Add(XmlNodeToEntity(child)); } return items; } private static T XmlNodeToEntity(XmlNode node) { T item = new T(); if (node.NodeType == XmlNodeType.Element) { XmlElement element = (XmlElement)node; System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (XmlAttribute attr in element.Attributes) { string attrName = attr.Name.ToLower(); string attrValue = attr.Value.ToString(); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo) { if (pinfo != null) { string name = pinfo.Name.ToLower(); Type dbType = pinfo.PropertyType; if (name == attrName) { if (String.IsNullOrEmpty(attrValue)) continue; switch (dbType.ToString()) { case "System.Int32": pinfo.SetValue(item, Convert.ToInt32(attrValue), null); break; case "System.Boolean": pinfo.SetValue(item, Convert.ToBoolean(attrValue), null); break; case "System.DateTime": pinfo.SetValue(item, Convert.ToDateTime(attrValue), null); break; case "System.Decimal": pinfo.SetValue(item, Convert.ToDecimal(attrValue), null); break; case "System.Double": pinfo.SetValue(item, Convert.ToDouble(attrValue), null); break; default: pinfo.SetValue(item, attrValue, null); break; } continue; } } } } } return item; } #endregion } }