using System;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.Data;
using System.Collections.Generic; namespace IOSerialize.Serialize
{
public static class xHelper
{
/// <summary>
/// 实体转化为XML
/// </summary>
public static string ParseToXml<T>(this T model, string fatherNodeName)
{
var xmldoc = new XmlDocument();
var modelNode = xmldoc.CreateElement(fatherNodeName);
xmldoc.AppendChild(modelNode); if (model != null)
{
foreach (PropertyInfo property in model.GetType().GetProperties())
{
var attribute = xmldoc.CreateElement(property.Name);
if (property.GetValue(model, null) != null)
attribute.InnerText = property.GetValue(model, null).ToString();
//else
// attribute.InnerText = "[Null]";
modelNode.AppendChild(attribute);
}
}
return xmldoc.OuterXml;
} /// <summary>
/// XML转换为实体,默认 fatherNodeName="body"
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml"></param>
/// <param name="fatherNodeName"></param>
/// <returns></returns>
public static T ParseToModel<T>(this string xml, string fatherNodeName = "body") where T : class ,new()
{ if (string.IsNullOrEmpty(xml))
return default(T);
var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
T model = new T();
var attributes = xmldoc.SelectSingleNode(fatherNodeName).ChildNodes;
foreach (XmlNode node in attributes)
{
foreach (var property in model.GetType().GetProperties().Where(property => node.Name == property.Name))
{
if (!string.IsNullOrEmpty(node.InnerText))
{
property.SetValue(model,
property.PropertyType == typeof(Guid)
? new Guid(node.InnerText)
: Convert.ChangeType(node.InnerText, property.PropertyType));
}
else
{
property.SetValue(model, null);
}
}
}
return model;
} /// <summary>
/// XML转实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml"></param>
/// <param name="headtag"></param>
/// <returns></returns>
public static List<T> XmlToObjList<T>(this string xml, string headtag)
where T : new()
{ var list = new List<T>();
XmlDocument doc = new XmlDocument();
PropertyInfo[] propinfos = null;
doc.LoadXml(xml);
XmlNodeList nodelist = doc.SelectNodes(headtag);
foreach (XmlNode node in nodelist)
{
T entity = new T();
if (propinfos == null)
{
Type objtype = entity.GetType();
propinfos = objtype.GetProperties();
}
foreach (PropertyInfo propinfo in propinfos)
{
//实体类字段首字母变成小写的
string name = propinfo.Name.Substring(, ) + propinfo.Name.Substring(, propinfo.Name.Length - );
XmlNode cnode = node.SelectSingleNode(name);
string v = cnode.InnerText;
if (v != null)
propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null);
}
list.Add(entity); }
return list;
}
}
}

最新文章

  1. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数
  2. Entity Framework 摘记
  3. cookie实现自动登录
  4. MySQL编译安装错误:No curses/termcap library found的解决方法
  5. 后缀数组 --- HDU 3518 Boring counting
  6. (转) DockPanel 右键增加关闭,除此之外全部关闭的功能
  7. How to save/read file on different platforms
  8. zabbix通过脚本发送短信
  9. js中的正则表达式入门
  10. 分而治之(Work Breakdown Structure, WBS)
  11. django框架 - 环境的搭建
  12. [.NET] 使用ValidationContext快速进行模型资料的验证
  13. gperftools对程序进行分析
  14. 爬虫实战——Scrapy爬取伯乐在线所有文章
  15. MySQL优化(二) 优化诀窍
  16. VEMap.DeleteAllShapeLayers 方法
  17. VirtualBox安装增强工具方法
  18. [原]vue - webapp 返回无效 解决方案
  19. 【AtCoder】ARC098题解
  20. python packages prebuild for windows

热门文章

  1. 转载:扒一扒Profiler中这几个“占坑鬼”
  2. Netty 学习资料
  3. linux shell创建目录、遍历子目录
  4. Java读写HDFS文件
  5. SpringBoot进阶之web进阶
  6. 深入理解java虚拟机读后总结(个人总结记录)
  7. centos中nodejs npm安装cordova
  8. 关于div
  9. Base64 转 图片
  10. java线程调度原则