1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 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 ; } } } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本