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
- XML帮助类 xml 转实体 或 实体 转xml
- java实体转xml
- XML实体
- XML 实体
- XML实体
- xml实体
- (转).NET+实体类反射得到XML(序列化为XML)
- 泛型实体集合转xml
- xml 序列化 实体类
- hibernate 实体类xml配置
- 实体转Xml存储和遍历所有Xml节点
- java实体类与xml数据互转
- 实体类JavaBean转Xml-JAXB的使用分享
- 实体类JavaBean转Xml-JAXB的使用分享
- xml中的实体参考
- XML的实体--ENTITY
- XML预定义实体
- XML 实体引用
- 问题:关于贴友分类菜单的实现
- 国庆游客爆棚内景区门票价格全球领先(图)
- zookeeper工作原理解析
- stl__map
- cocos2dx 3.x场景切换特效大全
- XML帮助类 xml 转实体 或 实体 转xml
- 类的继承笔记
- Python常用内建函数集
- LeetCode刷题笔录Spiral Matrix II
- Structured Exception Handling(SEH)
- VBO与VAO
- [Leetcode] Insert Interval
- 1.网络工具:ifconfig,ping,netstate,Redhat命令和图形化设置ip,finger,nslookup
- 黑马程序员_gets()和scanf()