using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1.AppCode
{
using System.Reflection;
using System.Xml; public class XmlTools
{
static XmlHelper xmlHelper = new XmlHelper(); /// <summary>
/// 将节点属性 转换成 Dictionary<string, string>
/// </summary>
/// <param name="nodeName"></param>
/// <returns></returns>
public static Dictionary<string, string> GetNodeAttrDict(XmlNode node)
{
Dictionary<string, string> dict = new Dictionary<string, string>(); if (node != null)
{
foreach (XmlAttribute attr in node.Attributes)
{
dict.Add(attr.Name, attr.Value);
}
}
return dict;
} /// <summary>
/// 获取多个节点
/// </summary>
/// <param name="xPath"></param>
/// <returns></returns>
public static List<XmlNode> GetXmlNodes(string xPath)
{
return xmlHelper.GetXmlNodeList(xPath);
}
/// <summary>
/// 获取单个节点
/// </summary>
/// <param name="xPath"></param>
/// <returns></returns>
public static XmlNode GetXmlNode(string xPath)
{
return xmlHelper.GetXmlNode(xPath);
}
/// <summary>
/// 节点转换成实体列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="nodes"></param>
/// <returns></returns>
public static List<T> ConvertXml2Entity<T>(List<XmlNode> nodes) where T : class,new()
{
return xmlHelper.GetEntityListByXmlNode<T>(nodes);
} /// 节点转换为实体
/// 注意:没有匹配到的字段为NULL
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="node"></param>
/// <returns></returns>
public static T ConvertXml2Entity<T>(XmlNode node) where T : class,new()
{
return xmlHelper.GetEntityByXmlNode<T>(node);
} /// <summary>
/// 获取节点文本内容
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public static string GetNoteTxt(XmlNode node)
{
return node != null ? node.InnerText : "";
}
/// <summary>
/// 获取节点整数文本
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public static int GetNode2Int(XmlNode node)
{
int _result = 0;
if (node != null)
{
int.TryParse(node.InnerText.Trim(), out _result);
}
return _result;
} } public class XmlHelper
{
XmlDocument xmlDoc = new XmlDocument();
public XmlHelper()
{
string xmlPath = HttpRuntime.AppDomainAppPath + "\\Files\\BaseConfig.xml";
xmlDoc.Load(xmlPath);
} public List<XmlNode> GetXmlNodeList(string nodeName)
{
List<XmlNode> list = new List<XmlNode>();
XmlNodeList nodeList = xmlDoc.SelectNodes(nodeName);
foreach (XmlNode node in nodeList)
{
list.Add(node);
}
return list;
} public XmlNode GetXmlNode(string nodeName)
{
return xmlDoc.SelectSingleNode(nodeName);
} /// <summary>
/// 将节点属性 转换成 Dictionary<string, string>
/// </summary>
/// <param name="nodeName"></param>
/// <returns></returns>
public static Dictionary<string, string> GetNodeAttrDict(XmlNode node)
{
Dictionary<string, string> dict = new Dictionary<string, string>(); if (node != null)
{
foreach (XmlAttribute attr in node.Attributes)
{
dict.Add(attr.Name, attr.Value);
}
}
return dict;
} public List<T> GetEntityListByXmlNode<T>(List<XmlNode> nodeList) where T : class,new()
{
List<T> list = new List<T>();
foreach (XmlNode item in nodeList)
{
list.Add(GetEntityByXmlNode<T>(item));
}
return list;
} public T GetEntityByXmlNode<T>(XmlNode node) where T : class,new()
{
return ConvertXml2Entity<T>(node);
} private T ConvertXml2Entity<T>(XmlNode node) where T : class,new()
{
if (node == null)
{
return default(T);
}
var entity = new T();
List<PropertyInfo> propsList = entity.GetType().GetProperties().ToList(); foreach (PropertyInfo prop in propsList)
{
var nodeObj = node.Attributes[prop.Name];
if (nodeObj != null)
{
var objValue = GetPropValue(prop.PropertyType.Name, nodeObj.Value);
prop.SetValue(entity, objValue, null);
}
} return entity;
} private object GetPropValue(string propName, string data)
{
object obj = data;
switch (propName)
{ case "DateTime":
obj = DateTime.Parse(data);
break;
case "Boolean":
obj = Boolean.Parse(data);
break;
case "Int32":
obj = int.Parse(data);
break;
case "Int64":
obj = long.Parse(data);
break;
case "Double":
obj = double.Parse(data);
break;
} return obj;
} }
}

  

最新文章

  1. iOS中数据库应用基础
  2. ubuntu下更改鼠标移动速度
  3. problem-eclipse创建maven项目报错
  4. Flexbox实现垂直水平居中
  5. [翻译] java NIO 教程---介绍
  6. [转]JDK6和JDK7中的substring()方法
  7. Ubuntu打开终端和设置root密码(转载)
  8. AVPicture、AVFrame和AVPacket
  9. 向MyEclipse中导入项目要注意的问题
  10. COJN 0584 800603吃糖果
  11. Redis的入门Demo(java)
  12. angular.isObject()
  13. LoadRunner Vuser接口测试脚本 Post举例
  14. React-router4 第六篇 No Match 404
  15. .NET Core微服务之路:文章系列和内容索引汇总 (v0.52)
  16. sublime 将打字内容放在屏幕中央
  17. Kiss MySQL goodbye for development and say hello to HSQLDB
  18. 报错: unable to register MBean
  19. 基于数据库构建分布式的ID生成方案
  20. 不能与abstruct共同修饰方法的关键字

热门文章

  1. IOC容器Autofac使用
  2. IOC控制反转之Autofac
  3. Git学习总结(14)——Git使用前的注意事项
  4. BNUOJ 1260 Brackets Sequence
  5. Linux下汇编语言学习笔记77 ---
  6. Codeforces Round #403(div 2)
  7. Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率
  8. 这篇讲angular 的$q的讲得不错
  9. Cannot update identity column &amp;#39;XXX&amp;#39;
  10. GRANT 授权