只能处理简单结构XML 和 实体。

using System.Text;
using System.Xml; namespace A.Util
{
public static class MyXmlUtil
{
public static string ModelToXml<T>(T a ,string xmlRootName)
{
StringBuilder scXml = new StringBuilder();
scXml.AppendFormat("<{0}>", xmlRootName); #region 主体
System.Reflection.PropertyInfo[] cfgItemProperties = a.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (System.Reflection.PropertyInfo item in cfgItemProperties)
{
string name = item.Name;
object value = item.GetValue(a, null);
//string 或 值属性,且value 不为 null
if ((item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && value != null && !string.IsNullOrEmpty(value.ToString()))
{
scXml.AppendFormat("<{0}>{1}</{0}>", name, value.ToString());
}
} #endregion scXml.AppendFormat("</{0}>", xmlRootName); string xml = scXml.ToString(); return xml;
} public static void XmlToModel<T>(T a,string xmlContent, string xmlRootName)
{
xmlContent = xmlContent.Trim(); //去除XML HEADER,否则LoadXml 时出错
if (xmlContent.Contains("<?"))
{
int bb = xmlContent.IndexOf('>');
xmlContent = xmlContent.Substring(bb + );
} XmlDocument xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null;//2018-12-3
xmlDoc.LoadXml(xmlContent);
XmlNode root = xmlDoc.SelectSingleNode(xmlRootName);
XmlNodeList xnl = root.ChildNodes; System.Reflection.PropertyInfo[] cfgItemProperties = a.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (XmlNode xnf in xnl)
{
//xnf.Name, xnf.InnerText
if (string.IsNullOrEmpty(xnf.InnerText))
{
continue;
} #region 主体 foreach (System.Reflection.PropertyInfo item in cfgItemProperties)
{
string name = item.Name;
//string 或 值属性
if ( item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String") )
{
if (item.Name == xnf.Name )
{
item.SetValue(a, xnf.InnerText, null);
}
}
} #endregion
} }
}
}

--

使用

VersChkHttpRsp vchrsp = new VersChkHttpRsp();
MyXmlUtil.XmlToModel<VersChkHttpRsp>(vchrsp, rspXml, "xml");

-

C#.NET XML 与 实体 MODEL 互转,非序列化的更多相关文章

  1. C# 不是序列化xml 转实体Model【原家独创】

    public static T XmlConvertModel<T>(string xmlStr) where T : class, new()        {            T ...

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

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

  3. XML与 实体的相互转化

    using System; using System.Linq; using System.Xml; using System.Reflection; using System.Data; using ...

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

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

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

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

  6. XML和对象属性互转的基类

    本人做了一个XML和对象属性互转的基类,现在放上来有兴趣拿去看一下,用法很简单,声明一个BaseConversion的子类,该子类与你想转换的对象相对应,然后覆盖基类的两个虚方法,然后在里面写元素与对 ...

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

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

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

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

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

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

随机推荐

  1. Css背景设置 、

    每天进步一小步,一年进步一大步. 第一次发博客园文章,主要记录自己学习的一个过程. CSS3 背景 CSS3 包含多个新的背景属性,它们提供了对背景更强大的控制. background-size ba ...

  2. PL/SQL Developer 快捷键

    前面我有分享了一个PLSQL美化规则,其实通过统一的美化SQL,把这里SQL写在Java代码里可以比较容易阅读代码,且保持良好得编码风格. 在工作中我们也经常使用PLSQL来写一SQL,有些常用的SQ ...

  3. 12、Python函数高级(命名空间、作用域、装饰器)

    一.名称空间和作用域 1.命名空间(Namespace) 命名空间是从名称到对象的映射,大部分的命名空间都是通过 Python 字典来实现的. 命名空间提供了在项目中避免名字冲突的一种方法.各个命名空 ...

  4. appium报错:An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET

    Appium Desktop版本:1.9.0 xcode版本:9.4.1 测试机:iPhone7  11.3系统 问题描述:在xcode上的produc的text运行是可以将WebDriverAgen ...

  5. PinPoint使用教程

    选择该应用的展示边界 InBound:3 OutBound:3 基本概念 APM (Application Performance Management/应用性能管理)工具 为大规模分布式系统. 开发 ...

  6. memoryDiary

    What did you accomplish today? , did you exercise today? Do you care about the people around you tod ...

  7. LeetCode 301. Remove Invalid Parentheses

    原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...

  8. UFUN函数 UF_ATTR函数(UF_ATTR_read_value 函数用法)

    //此函数的功能是输入tag值,返回与属性标题对应的属性值 static string read_attr(tag_t object_tag) { UF_initialize(); ]="零 ...

  9. 上传OSS报错

    修改OSS

  10. 洛谷p3398仓鼠找suger题解

    我现在爱死树链剖分了 题目 具体分析的话在洛谷blog里 这里只是想放一下改完之后的代码 多了一个son数组少了一个for 少了找size最大的儿子的for #include <cstdio&g ...