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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 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>
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个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搭建本