zoukankan      html  css  js  c++  java
  • 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;
            }
    
    
        }
    }
    

      

  • 相关阅读:
    如何用一句话激怒设计师
    Kubernetes1.3:POD生命周期管理
    从零学React Native之11 TextInput
    从零学React Native之10Text
    html+js 在页面同步服务器时间
    电信行业的容器化改造之道
    容器化ICT融合初体验
    Laravel 发送邮件(最简单的讲解!)
    jquery( 点击按钮出来文本框并限制文本框的个数)
    OpenStack宣布用Kubernetes重写底层编排引擎
  • 原文地址:https://www.cnblogs.com/zjflove/p/4750636.html
Copyright © 2011-2022 走看看