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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值