XML帮助类 xml 转实体 或 实体 转xml

来源:互联网 发布:百川考试软件官方网站 编辑:程序博客网 时间:2024/05/07 11:50

XML帮助类 xml 转实体 或 实体 转xml 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Linq;using System.Reflection;using System.Collections;namespace ClassLibrary1{    public static class XMLHelper    {        public static void FillEntityWithXml<T>(this T entity, XDocument doc, Assembly assembly, AssemblyLoadType LoadType = AssemblyLoadType.Calling, string AssemblyName = null)                                       where T : class, new()        {            if (assembly == null)            {                switch (LoadType)                {                    case AssemblyLoadType.Calling:                        assembly = Assembly.GetCallingAssembly();                        break;                    case AssemblyLoadType.Executing:                        assembly = Assembly.GetExecutingAssembly();                        break;                    case AssemblyLoadType.Load:                        assembly = Assembly.Load(AssemblyName);                        break;                }            }            entity = entity ?? new T();            XElement root = doc.Root;            PropertyInfo[] props = entity.GetType().GetProperties();            foreach (PropertyInfo prop in props)            {                var propName = prop.Name;                if (root.Element(propName) != null)                {                    string XMLValue = root.Element(propName).Value;                    switch (prop.PropertyType.Name)                    {                        #region 基本属性                        case "DateTime":                            DateTime dtValue;                            if (DateTime.TryParse(XMLValue, out dtValue))                            {                                prop.SetValue(entity, dtValue, null);                            }                            break;                        case "Boolean":                            bool bValue = false;                            if (bool.TryParse(XMLValue, out bValue))                            {                                prop.SetValue(entity, bValue, null);                            }                            break;                        case "Int32":                            int iValue = 0;                            if (int.TryParse(XMLValue, out iValue))                            {                                prop.SetValue(entity, iValue, null);                            }                            break;                        case "Int64":                            long lValue = 0;                            if (long.TryParse(XMLValue, out lValue))                            {                                prop.SetValue(entity, lValue, null);                            }                            break;                        case "Double":                            double dValue = 0;                            if (double.TryParse(XMLValue, out dValue))                            {                                prop.SetValue(entity, dValue, null);                            }                            break;                        case "Single":                            float fValue = 0;                            if (Single.TryParse(XMLValue, out fValue))                            {                                prop.SetValue(entity, fValue, null);                            }                            break;                        case "String":                            prop.SetValue(entity, XMLValue, null);                            break;                        #endregion                        #region List                        case "List`1":                            string itemName = prop.PropertyType.GetGenericArguments()[0].FullName;                            Type Ttype = assembly.GetType(itemName);                            Type listType = typeof(List<>).MakeGenericType(Ttype);                            var list = Activator.CreateInstance(listType);                            MethodInfo addMethod = listType.GetMethod("Add");                            foreach (XElement xe in root.Element(propName).Elements())                            {                                var itemEntity = assembly.CreateInstance(itemName);                                FillEntityWithXml(itemEntity, new XDocument(xe), assembly);                                addMethod.Invoke((object)list, new object[] { itemEntity });                            }                            prop.SetValue(entity, list, null);                            break;                        #endregion                        #region 类或枚举                        default:                            if (prop.PropertyType.IsEnum)                            {                                Type eType = assembly.GetType(prop.PropertyType.FullName);                                prop.SetValue(entity, Enum.Parse(eType, XMLValue), null);                            }                            else                            {                                var cEntity = assembly.CreateInstance(prop.PropertyType.FullName);                                FillEntityWithXml(cEntity, new XDocument(root.Element(propName)), assembly);                                prop.SetValue(entity, cEntity, null);                            }                            break;                        #endregion                    }                }            }        }        public static XElement XMLFromEntity<T>(this T entity)        {            Type type = entity.GetType();            XElement doc = new XElement(type.Name);            PropertyInfo[] props = type.GetProperties();            foreach (PropertyInfo prop in props)            {                XElement node = new XElement(prop.Name);                if (!prop.PropertyType.IsGenericType)                {                    object nodevalue = prop.GetValue(entity, null);                    node.Value = nodevalue == null ?"": nodevalue.ToString();                }                else {                    IList lstValue = prop.GetValue(entity, null) as IList;                    if (lstValue != null)                    {                        foreach (var childnode in lstValue)                        {                            node.Add(XMLFromEntity(childnode));                        }                    }                }                doc.Add(node);            }            return doc;        }    }}


0 0
原创粉丝点击