c# 操作xml文件(读写)
根据项目需求,我们需要记录用户的操作痕迹,当用户下次登录操作同一个文件时,页面显示为用户上一次执行的用户轨迹,我们考虑把用户的历史记录写进xml文件中。
存储的xml数据结构:
XML操作类:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Xml; 8 9 namespace iImgEnh.UI.AuthenticateImage.AuthImageMethods.Model 10 { 11 public class VideoContinuityReadWriteRecord 12 { 13 #region 属性 14 /// <summary> 15 /// 存放xml内容的文件夹下的文件名 16 /// </summary> 17 private string DicFileName = "resources/XMLFile.xml"; 18 19 #endregion 20 21 #region 写入xml 22 /// <summary> 23 /// 24 /// </summary> 25 /// <param name="file"></param> 26 /// <param name="content"></param> 27 /// <param name="dateTimeStr"></param> 28 public void WriteDoc(string file,string content,string dateTimeStr) 29 { 30 string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "resources"); 31 32 //判断是否存在文件夹 33 var DirectoryPath = Path.GetDirectoryName(SavePath); //获取文件夹所在的路径 34 if (!Directory.Exists(SavePath)) 35 { 36 Directory.CreateDirectory(SavePath); //创建文件夹 37 } 38 XmlDocument doc = new XmlDocument(); 39 if (File.Exists(DicFileName)) 40 { 41 //如果文件存在 加载XML 42 doc.Load(DicFileName); 43 //获得文件的根节点 44 XmlNodeList xnl = doc.SelectNodes("/Positions/Position/Item"); 45 if (xnl.Count < 1) 46 { 47 CreateDoc(file, content,dateTimeStr); 48 } 49 else 50 { 51 XmlNode PNode = null; 52 var isHave = false; 53 foreach (XmlNode item in xnl) 54 { 55 PNode = item.ParentNode; 56 var name = item.Attributes["Name"].Value; 57 //var text = item.Attributes["Content"].Value; 58 if (name == file) 59 { 60 isHave = true; 61 item.Attributes["Content"].Value = content; 62 item.Attributes["Time"].Value = dateTimeStr; 63 break; 64 } 65 } 66 if (!isHave) 67 { 68 var en = doc.DocumentElement; 69 XmlElement name1 = doc.CreateElement("Item"); 70 name1.SetAttribute("Name", file); 71 name1.SetAttribute("Content", content); 72 name1.SetAttribute("Time", dateTimeStr); 73 PNode.AppendChild(name1); 74 75 if (xnl.Count > 20) 76 { 77 PNode.RemoveChild(xnl[0]); 78 } 79 80 } 81 } 82 doc.Save(DicFileName); 83 } 84 else 85 { 86 CreateDoc(file, content,dateTimeStr); 87 } 88 } 89 90 /// <summary> 91 /// 读取XML文件 92 /// </summary> 93 /// <param name="file"></param> 94 /// <returns></returns> 95 public RecordInfo ReadDoc(string file) 96 { 97 RecordInfo info = new RecordInfo(); 98 XmlDocument doc = new XmlDocument(); 99 if (File.Exists(DicFileName)) 100 { 101 //如果文件存在 加载XML 102 doc.Load(DicFileName); 103 //获得文件的根节点 104 XmlNodeList xnl = doc.SelectNodes("/Positions/Position/Item"); 105 if (xnl.Count > 0) 106 { 107 foreach (XmlNode item in xnl) 108 { 109 if (item.Attributes["Name"].Value == file) 110 { 111 info.Position = item.Attributes["Content"].Value; 112 info.Time = item.Attributes["Time"].Value; 113 } 114 } 115 } 116 } 117 return info; 118 } 119 120 /// <summary> 121 /// 创建XML文件 122 /// </summary> 123 /// <param name="file"></param> 124 /// <param name="content"></param> 125 /// <param name="dateTimeStr"></param> 126 private void CreateDoc(string file, string content,string dateTimeStr) 127 { 128 XmlDocument doc = new XmlDocument(); 129 //3、创建第一个行描述信息,并且添加到doc文档中 130 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); 131 doc.AppendChild(dec); 132 //4、创建根节点 133 XmlElement books = doc.CreateElement("Positions"); 134 //将根节点添加到文档中 135 doc.AppendChild(books); 136 137 //5、给根节点Books创建子节点 138 XmlElement book1 = doc.CreateElement("Position"); 139 //将book添加到根节点 140 books.AppendChild(book1); 141 //6、给Book1添加子节点 142 XmlElement name1 = doc.CreateElement("Item"); 143 name1.SetAttribute("Name", file); 144 name1.SetAttribute("Content", content); 145 name1.SetAttribute("Time", dateTimeStr); 146 book1.AppendChild(name1); 147 148 doc.Save(DicFileName); 149 } 150 151 #endregion 152 } 153 154 public class RecordInfo 155 { 156 /// <summary> 157 /// 位置 158 /// </summary> 159 public string Position { get; set; } 160 161 /// <summary> 162 /// 时间格式 163 /// </summary> 164 public string Time { get; set; } 165 } 166 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】