• XmlHelper


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace ZB.QueueSystem.Common
    {
        /// <summary>
        /// 用于XML操作
        /// </summary>
        public class XmlHelper
        {
            private static XmlHelper instance;
            public static XmlHelper Instance
            {
                get
                {
                    if (instance == null) instance = new XmlHelper();
                    return XmlHelper.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>();
                foreach (XmlNode item in node)
                {
                    value = item.Attributes[attribute].Value;
                    // if (string.IsNullOrEmpty(value)) continue;
    
                    list.Add(value);
                }
    
                return list;
            }
    
            /// <summary>
            /// 获取config.xml文件root/am或者root/pm节点下item节点对应属性集合
            /// </summary>
            /// <param name="path">文件路径</param>
            /// <param name="nodePath">节点路径</param>
            /// <returns>List<ConfigItem></returns>
            public List<ConfigItem> GetNodeItemList(string path, string nodePath)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNode node = doc.SelectSingleNode(nodePath);
                string value = string.Empty;
                List<ConfigItem> list = new List<ConfigItem>();
                foreach (XmlNode item in node)
                {
                    ConfigItem config = new ConfigItem();
                    config.Content = item.Attributes["content"].Value;
                    config.BeginCode = item.Attributes["begincode"].Value;
                    config.EndCode = item.Attributes["endcode"].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>
            /// 添加节点
            /// </summary>
            /// <param name="path">文件路径</param>
            /// <param name="nodePath">节点路径</param>
            /// <param name="items">List<ConfigItem> items</param>
            /// <returns></returns>
            public bool AddNodeByNode(string path, string nodePath, List<ConfigItem> items)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
    
                XmlNode node = doc.SelectSingleNode(nodePath);
                foreach (ConfigItem item in items)
                {
                    XmlElement element = doc.CreateElement("item");
                    element.SetAttribute("content",item.Content);
                    element.SetAttribute("begincode", item.BeginCode);
                    element.SetAttribute("endcode", item.EndCode);
    
                    node.AppendChild(element);
                }
    
                doc.Save(path);
    
                return true;
            }
    
            /// <summary>
            /// 对指定节点内容进行替换
            /// </summary>
            /// <param name="path">文件路径</param>
            /// <param name="nodePath">节点路径</param>
            /// <param name="items">List<ConfigItem> items</param>
            /// <returns></returns>
            public bool ResetNodeByNode(string path, string nodePath, List<ConfigItem> items)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
    
                XmlNode node = doc.SelectSingleNode(nodePath);
                node.InnerText = string.Empty;
    
                foreach (ConfigItem item in items)
                {
                    XmlElement element = doc.CreateElement("item");
                    element.SetAttribute("content", item.Content);
                    element.SetAttribute("begincode", item.BeginCode);
                    element.SetAttribute("endcode", item.EndCode);
    
                    node.AppendChild(element);
                }
    
                doc.Save(path);
    
                return true;
            }
        }
    
        /// <summary>
        /// config.xml item节点属性描述
        /// </summary>
        public class ConfigItem
        {
            public string Content { get; set; }
            public string BeginCode { get; set; }
            public string EndCode { get; set; }
        }
    }
    

      

    <?xml version="1.0" encoding="utf-8"?>
    <root>
    <test begintime="8" endtime="12" colcount="15" codecount="100">
        <item content=" 08:00-09:00" begincode="1" endcode="25"/>
        <item content=" 09:00-10:00" begincode="25" endcode="50"/>
        <item content=" 10:00-11:00" begincode="50" endcode="75"/>
        <item content=" 11:00-12:00" begincode="75" endcode="100"/>
      </test>
    </root>
  • 相关阅读:
    MVC系列-7.更新
    MVC系列-6.注册页面
    MVC系列-5.详细信息
    MVC系列-4.布局页
    MVC系列-3.数据显示
    MVC系列-2.数据处理-登陆
    MVC系列-1.MVC入门
    bootstrap table 服务器端分页--ashx+ajax
    swift学习第八天:元组
    swift学习第七天:字典
  • 原文地址:https://www.cnblogs.com/YYkun/p/7196469.html
Copyright © 2020-2023  润新知