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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM