• Xml的读取


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

      

  • 相关阅读:
    gbin1分享: jQuery UI 1.9带给我们的惊喜
    7个优秀的javascript资源
    转载:仅需78美元,你就能拥有一个iPhone机器人Romo
    GBin1推荐:使用时间轴插件Timelinr创建超酷jQuery时间轴(Time line)
    GBin1分享:10个帮助你精通Firebug控制台的技巧
    GBin1分享:jQuery1.7 Beta 预览
    批处理文件安装卸载window服务程序的技巧
    当前不会命中断点 还没有为该文档加载任何符号
    HttpModule,HttpHandler,HttpHandlerFactory简单使用
    VS2010 .net Windows服务程序 重复性 注册反注册
  • 原文地址:https://www.cnblogs.com/zjflove/p/4750636.html
Copyright © 2020-2023  润新知