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

XML与 实体的相互转化的更多相关文章

  1. c# XML和实体类之间相互转换(序列化和反序列化)[砖]

    link: http://blog.okbase.net/haobao/archive/62.html by: 好饱 我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlU ...

  2. C# XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...

  3. 利用MyEclipse连接数据库并自动生成基于注解或者XML的实体类

    一.利用MyEclipse连接数据库 1. 打开MyEclipse的数据库连接视图 然后在Other中找到"MyEclipse Database"中的DB Browser 2. 在 ...

  4. XML外部实体注入漏洞(XXE)

    转自腾讯安全应急响应中心 一.XML基础知识 XML用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.XML文档结构包括XML声 ...

  5. XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  6. XXE(XML External Entity attack)XML外部实体注入攻击

    导语 XXE:XML External Entity 即外部实体,从安全角度理解成XML External Entity attack 外部实体注入攻击.由于程序在解析输入的XML数据时,解析了攻击者 ...

  7. 微信支付的JAVA SDK存在漏洞,可导致商家服务器被入侵(绕过支付)XML外部实体注入防护

    XML外部实体注入 例: InputStream is = Test01.class.getClassLoader().getResourceAsStream("evil.xml" ...

  8. 【代码审计】CLTPHP_v5.5.3前台XML外部实体注入漏洞分析

    0x01 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chichu/ ...

  9. XML外部实体(XXE)注入详解

    ###XML与xxe注入基础知识 1.XMl定义 XML由3个部分构成,它们分别是:文档类型定义(Document Type Definition,DTD),即XML的布局语言:可扩展的样式语言(Ex ...

随机推荐

  1. nginx与Apache的优缺点

    来源:http://itindex.net/detail/46414-apache-nginx 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资 ...

  2. 05机器学习实战之Logistic 回归scikit-learn实现

    https://blog.csdn.net/zengxiantao1994/article/details/72787849似然函数 原理:极大似然估计是建立在极大似然原理的基础上的一个统计方法,是概 ...

  3. A* 寻路学习

    启发式搜索:启发式搜索就是在状态空间中的搜索.对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省略大量无谓的搜索路径,提高了效率.在启发式搜索中,对位置的估价是十分重 ...

  4. python中in,not in,比较运算符,格式化输出,编码

    一,python中的in,和not in python中in的作用是检测或查找,例如: c = ‘你好大号胡覅但是啊飞碟说’ b = ‘你好’ print(b in c ) 结果: True c = ...

  5. C# string 转 bool

    bool _b = Convert.ToBoolean("False"); "_b => false" // // 摘要: //     将逻辑值的指定字 ...

  6. Apache 配置Https 转发Tomcat Http

    Apache 相对于nginx的配置对比起来相当复杂啦,朋友之前的系统使用的是Apache需要增加一个虚拟主机,主要配置从Apache转发Tomcat. 首先需要拆解下步骤: Apache 支持Htt ...

  7. LOJ 3055 「HNOI2019」JOJO—— kmp自动机+主席树

    题目:https://loj.ac/problem/3055 先写了暴力.本来想的是 n<=300 的那个在树上暴力维护好整个字符串, x=1 的那个用主席树维护好字符串和 nxt 数组.但 x ...

  8. 代码问题: 【MatConvNet库编译】

    问题1: 老版本的MatConvNet在编译对cuDNN支持的时候,cuDNN的版本是2或者4比较好,官网有明确的描述. 比如我编译用了cuDNN 6.0的版本,在编译 nnconv_cudnn.cu ...

  9.  WPF 控件总结

    内容控件:1.Button:当Button.IsCancel="True"时,点击按钮,对话框关闭.当Button.IsDefault="True",按回车触发 ...

  10. 推荐四款 Bug 管理系统,最适合你的才是最好的!

    转载自:https://www.jianshu.com/p/e7d3121eaaec   在这个移动互联网的时代,每天都会涌入大量新的 App,想要留住你的用户,必须时刻保持产品创新与系统的稳定.对于 ...