XML 操作

using System;
using System.Collections.Generic;
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>
        /// 根据路径、节点、属性获取属性对应的值
        /// </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 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; }
    }
}

  

博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
原文地址:https://www.cnblogs.com/YYkun/p/14549826.html