首页 > 其他分享 >XmlHelper

XmlHelper

时间:2022-10-28 18:14:57浏览次数:34  
标签:node string doc XmlHelper new path public

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ZB.QueueSys.Common
{
    /// <summary>
    /// 用于XML操作
    /// </summary>
    public class XmlHelper
    {
        //定义一个用于保存静态变量的实例
        private static XmlHelper instance = null;
        //定义一个保证线程同步的标识
        private static readonly object locker = new object();
        //构造函数为私有,使外界不能创建该类的实例
        private XmlHelper() { }
        public static XmlHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null) instance = new XmlHelper();
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// type:0,解析xml类型的value
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public DataSet XmlToDataSet(string xml)
        {
            DataSet set = new DataSet();
            StringReader stream = new StringReader(xml);//读取字符串为数据量
            XmlTextReader reader = new XmlTextReader(stream);//对XML的数据流的只进只读访问
            set.ReadXml(reader);//把数据读入DataSet
            return set;
        }

        /// <summary>
        /// 读取xml
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public DataSet GetXml(string xml)
        {
            DataSet set = new DataSet();
            StringReader stream = new StringReader(xml);//读取字符串为数据量
            XmlTextReader reader = new XmlTextReader(stream);//对XML的数据流的只进只读访问
            set.ReadXml(reader);//吧数据读入DataSet
            return set;
        }

        /// <summary>
        /// 根据路径、节点、属性获取属性对应的值
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="nodeName">节点路径,eg:root//am</param>
        /// <param name="attributeName">属性名称</param>
        /// <returns>属性值</returns>
        public string GetAttributeValueByXmlNode(string path, string nodeName, string attributeName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            XmlNode node = doc.SelectSingleNode(nodeName);
            return node.Attributes[attributeName].Value;
        }

        /// <summary>
        /// 根据路径、节点、属性设置属性对应的值
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="nodeName">节点路径,eg:root//am</param>
        /// <param name="attributeName">属性名称</param>
        /// <param name="attValue">属性值</param>
        public void SetAttributeValueByXmlNode(string path, string nodeName,
            string attributeName, string attValue)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            XmlNode node = doc.SelectSingleNode(nodeName);
            node.Attributes[attributeName].Value = attValue;
            doc.Save(path);
        }

        /// <summary>
        /// 获取指定节点子节点指定属性的值
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="nodePath">节点</param>
        /// <param name="attribute">属性名称</param>
        /// <returns>List<string>value属性的值</returns>
        public List<string> GetNodeItemAttValue(string path, string nodePath, string attribute)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNode node = doc.SelectSingleNode(nodePath);
            string value = string.Empty;
            List<string> list = new List<string>();
            if (node == null) return list;
            foreach (XmlNode item in node)
            {
                value = item.Attributes[attribute].Value;
                list.Add(value);
            }

            return list;
        }


        public List<string> GetNodeRootAllItemAttValue(string path, string attribute)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            string nodePath = "Root";
            XmlNode node = doc.SelectSingleNode(nodePath);
            string value = string.Empty;
            List<string> list = new List<string>();
            if (node == null) return list;
            foreach (XmlNode temp in node.ChildNodes)
            {
                foreach (XmlNode item in temp)
                {
                    value = item.Attributes[attribute].Value;
                    list.Add(value);
                }
            }

            list = list.Distinct<string>().ToList();
            return list;
        }


        public string GetNodeValueByKey(string path, string nodePath, string direction)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNode node = doc.SelectSingleNode(nodePath);
            string value = string.Empty;
            if (node == null) return string.Empty;
            foreach (XmlNode item in node)
            {
                if (item.Attributes["ORGANMODE"].Value.Trim() != direction.Trim()) continue;

                return item.Attributes["REMARK"].Value;
            }

            return value;
        }

        /// <summary>
        /// 获取指定节点所有子节点相关属性
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="nodePath">节点路径</param>
        /// <returns>List<MenuDataItem></returns>
        public List<MenuItemNodes> GetMenuDataItems(string path, string selectNode)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNode node = doc.SelectSingleNode(selectNode);
            List<MenuItemNodes> list = new List<MenuItemNodes>();
            if (node == null) return list;
            foreach (XmlNode item in node)
            {
                MenuItemNodes config = new MenuItemNodes();
                config.Value = item.Attributes["content"].Value;
                config.Name = item.Attributes["uiname"].Value;
                config.ImagePath = item.Attributes["imagepath"].Value;

                list.Add(config);
            }

            return list;
        }

        /// <summary>
        /// 删除指定节点的子节点
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="nodePath">节点路径</param>
        /// <returns></returns>
        public bool RemoveNodeByNode(string path, string nodePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            XmlNode node = doc.SelectSingleNode(nodePath);
            node.InnerText = string.Empty;
            doc.Save(path);

            return true;
        }
    }

    /// <summary>
    /// config.xml item节点属性描述
    /// </summary>
    public class MenuItemNodes
    {
        public string Value { get; set; }
        public string Name { get; set; }
        public string ImagePath { get; set; }
    }
}

  

标签:node,string,doc,XmlHelper,new,path,public
From: https://www.cnblogs.com/YYkun/p/16836970.html

相关文章