////// 历史下载记录xml文件操作 /// public class XMLHelper { private string xmlFilePath = ""; ////// 历史下载记录xml文件操作 /// /// public XMLHelper(string xmlFilePath) { this.xmlFilePath = xmlFilePath; //检测xml文件 if (System.IO.File.Exists(xmlFilePath)) { try { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(xmlFilePath); } catch (Exception) { System.IO.File.Delete(xmlFilePath); CreateXml(); } } } private void CreateXml() { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.AppendChild(xmlDoc.CreateNode(System.Xml.XmlNodeType.XmlDeclaration, "", "")); System.Xml.XmlElement xmlE = xmlDoc.CreateElement("DownloadHistory"); xmlDoc.AppendChild(xmlE); xmlDoc.Save(xmlFilePath); } ////// 写入一个下载文件记录 /// /// 文件名 /// MD5值 /// 服务器ID /// 当前下载文件分块编号 /// 当前下载文件分块总数 public void WriteXml(string fileName, string md5, string serverId, string currentDownloadBlock, string totalBlock) { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(xmlFilePath); System.Xml.XmlNode rootNode = xmlDoc.CreateNode("element", "File", ""); System.Xml.XmlElement xmlE; System.Xml.XmlText xmlT; xmlE = xmlDoc.CreateElement("FileName"); xmlT = xmlDoc.CreateTextNode(fileName); rootNode.AppendChild(xmlE); rootNode.LastChild.AppendChild(xmlT); xmlE = xmlDoc.CreateElement("MD5"); xmlT = xmlDoc.CreateTextNode(md5); rootNode.AppendChild(xmlE); rootNode.LastChild.AppendChild(xmlT); xmlE = xmlDoc.CreateElement("ServerId"); xmlT = xmlDoc.CreateTextNode(serverId); rootNode.AppendChild(xmlE); rootNode.LastChild.AppendChild(xmlT); xmlE = xmlDoc.CreateElement("CurrentDownloadBlock"); xmlT = xmlDoc.CreateTextNode(currentDownloadBlock); rootNode.AppendChild(xmlE); rootNode.LastChild.AppendChild(xmlT); xmlE = xmlDoc.CreateElement("TotalBlock"); xmlT = xmlDoc.CreateTextNode(totalBlock); rootNode.AppendChild(xmlE); rootNode.LastChild.AppendChild(xmlT); //将节点添加到文档中 System.Xml.XmlElement root = xmlDoc.DocumentElement; root.AppendChild(rootNode); xmlDoc.Save(xmlFilePath); } ////// 删除一个下载文件记录 /// /// public void DeleteNode(string fileName) { try { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(this.xmlFilePath); foreach (System.Xml.XmlNode node in xmlDoc.SelectNodes("//FileName")) { if (node.InnerXml == fileName) { node.ParentNode.ParentNode.RemoveChild(node.ParentNode); } } xmlDoc.Save(this.xmlFilePath); } catch { }; } ////// 更新一个下载文件记录 /// /// 文件名 /// MD5值 /// 服务器ID /// 当前下载文件分块编号 /// 当前下载文件分块总数 public void UpdateXml(string fileName, string md5, string serverId, string currentDownloadBlock, string totalBlock) { try { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(this.xmlFilePath); System.Xml.XmlNodeList nodes = xmlDoc.SelectNodes("//File"); if (nodes != null) { System.Xml.XmlNode xNode; foreach (System.Xml.XmlNode xn in nodes) { xNode = xn.SelectSingleNode("FileName"); if (xNode != null) { if (xNode.InnerText == fileName) { xNode = xn.SelectSingleNode("CurrentDownloadBlock"); if (xNode != null) xNode.InnerText = currentDownloadBlock; break; } } } } else { WriteXml(fileName, md5, serverId, currentDownloadBlock, totalBlock); } xmlDoc.Save(this.xmlFilePath); } catch (System.IO.FileNotFoundException) { //文件未找到 CreateXml(); WriteXml(fileName, md5, serverId, currentDownloadBlock, totalBlock); } catch (System.Xml.XmlException) { //xml文件格式错误 System.IO.File.Delete(this.xmlFilePath); CreateXml(); WriteXml(fileName, md5, serverId, currentDownloadBlock, totalBlock); } catch (Exception ex) { throw ex; } } }
操作的XML文件结构如下:
录像1 4B48E3E2D7777B938A8C5BF39B55BEB9 123456 1000 3000