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

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;
        }
    }
}

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...