zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Error -26631: HTTP Status-Code=400 (Bad Request) for
    mysql中的制表符替换
    mysql中json数据的拼接方式
    使用Nightwatch.js做基于浏览器的web应用自动测试
    Selenium + Nightwatch 自动化测试环境搭建
    Python web 框架:web.py
    转 Python Selenium设计模式-POM
    自动化测试
    日志打印longging模块(控制台和文件同时输出)
    读取配置文件(configparser,.ini文件)
  • 原文地址:https://www.cnblogs.com/YYkun/p/7196469.html
Copyright © 2011-2022 走看看