According to project requirements, we need to record user activity traces, the next time the user login operation with a file, the page displayed to the user on the user's track once performed, we consider the history of the user's written into the xml file.
Xml data structures stored in:
XML operations categories:
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 /// storing xml content folder under the file name 16 /// </ Summary> . 17 Private String DicFileName = " Resources / Xmlfile.xml " ; 18 is . 19 #endregion 20 is 21 is #region write xml 22 is /// < Summary> 23 is /// 24 /// </ Summary> 25 /// <param name = "File"> </ param> 26 is /// <param name = "Content"> </ param> 27 /// <param name = "dateTimeStr"> </ param> 28 public void WriteDoc(stringFile, String Content, String dateTimeStr) 29 { 30 String SavePath = Path.Combine (Directory.GetCurrentDirectory (), " Resources " ); 31 is 32 // determines whether there is a folder 33 is var DirectoryPath = Path.GetDirectoryName (SavePath); / / Get the path to the folder where 34 is IF (! Directory.Exists (SavePath)) 35 { 36 Directory.CreateDirectory (SavePath); // create a folder 37 } 38 DOC = the XmlDocument new new the XmlDocument (); 39 IF (the File.Exists (DicFileName)) 40 { 41 is // If the file exists to load the XML 42 is doc.Load (DicFileName); 43 is // root file obtained 44 is an XmlNodeList XNL = DOC. the SelectNodes ( " / the Positions / the Position / Item " ); 45 IF (xnl.Count < . 1 ) 46 is { 47 CreateDoc (File, Content, dateTimeStr); 48 } 49 the 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 new RecordInfo (); 98 the XmlDocument DOC = new new the XmlDocument () ; 99 IF (the File.Exists (DicFileName)) 100 { 101 // If the file exists to load the XML 102 doc.Load (DicFileName); 103 // get the root file 104 an 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, stringContent, String dateTimeStr) 127 { 128 the XmlDocument doc = new new the XmlDocument (); 129 // . 3, creating a first description line, and added to the document doc 130. the XmlDeclaration On Dec = doc.CreateXmlDeclaration ( " 1.0 " , " UTF- . 8 " , null ); 131 is doc.AppendChild (On Dec); 132 // . 4, to create a root node 133 the XmlElement Books = doc.createElement ( " the Positions " ); 134 // root node added to the document 135 doc.AppendChild (Books); 136 137 // . 5, to create a child node of the root node Books 138 the XmlElement Book1 = doc.createElement ( " the Position " ); 139 // Add the book to the root node 140 books.AppendChild (Book1); 141 // 6, the child is added to Book1 142 the XmlElement NAME1 = doc.createElement ( " Item " ); 143 name1.SetAttribute ( " the Name " , File); 144 name1.SetAttribute ( " the 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 /// Time Format 163 /// </ Summary> 164 is public String Time { GET ; SET ;} 165 } 166 }