/// <summary>
/// 历史下载记录xml文件操作
/// </summary>
public class XMLHelper
{
private string xmlFilePath = "";
/// <summary>
/// 历史下载记录xml文件操作
/// </summary>
/// <param name="xmlFilePath"></param>
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);
} /// <summary>
/// 写入一个下载文件记录
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="md5">MD5值</param>
/// <param name="serverId">服务器ID</param>
/// <param name="currentDownloadBlock">当前下载文件分块编号</param>
/// <param name="totalBlock">当前下载文件分块总数</param>
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);
} /// <summary>
/// 删除一个下载文件记录
/// </summary>
/// <param name="fileName"></param>
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 { };
} /// <summary>
/// 更新一个下载文件记录
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="md5">MD5值</param>
/// <param name="serverId">服务器ID</param>
/// <param name="currentDownloadBlock">当前下载文件分块编号</param>
/// <param name="totalBlock">当前下载文件分块总数</param>
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文件结构如下:

<?xml version="1.0"?>
<DownloadHistory>
<File>
<FileName>录像1</FileName>
<MD5>4B48E3E2D7777B938A8C5BF39B55BEB9</MD5>
<ServerId>123456</ServerId>
<CurrentDownloadBlock>1000</CurrentDownloadBlock>
<TotalBlock>3000</TotalBlock>
</File>
</DownloadHistory>

XML读写文件辅助类的更多相关文章

  1. xml读写文件实例

    在某个通讯中需要向服务器发送请求xml,格式例子如下: <?xml version="1.0" encoding="UTF-8"?> <ROO ...

  2. OpenCV FileStorage类读写XML/YML文件

    本文转自:http://www.cnblogs.com/summerRQ/articles/2524560.html 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件, ...

  3. XML结构文件的读写

    附件:http://files.cnblogs.com/xe2011/XML_Writer_And_Read.rar 下面这段代码实现了以下功能 数据保存 textBox1的文本,textBox2的文 ...

  4. OpenCV教程(42) xml/yaml文件的读写

    参考资料: http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html #include "opencv2/openc ...

  5. Inno Setup 如何读写文件

    软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...

  6. Android用路径api在内部存储读写文件

    复制并修改原有项目 复制之前创建的项目CC+CV操作 需要改动的地方: * 项目名字 * 应用包名 * R文件重新导包 接着修改件/AndroidManifest.xml中的包名:package=&q ...

  7. Ibatis学习总结4--SQL Map XML 映射文件扩展

    SQL Map XML 映射文件除了上文提到的属性还有一些其他重要的属性,下文将详细介绍这些属性. 缓存 Mapped Statement 结果集 通过在查询 statement 中指定 cacheM ...

  8. android 学习随笔二(读写文件)

    在android读写文件 RAM:运行内存,相当于电脑的内存 ROM:内部存储空间,相当电脑硬盘,android手机必须有的 SD卡:外部存储空间,相当电脑的移动硬盘,不是必须的.手机如果内置16G存 ...

  9. 网站的配置文件XML读写

    网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考. 读操作: XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettin ...

随机推荐

  1. 用OO方式写键盘字母小游戏

    <html>  <head>   <title>0.0</title>   <script>    window.onload=functi ...

  2. shell学习笔记

    shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...

  3. C语言入门(1)——C语言概述

    1.程序与编程语言 我们使用计算机离不开程序,程序告诉计算机应该如何运行.程序(Program)是一个精确说明如何进行计算的指令序列.这里的计算可以是数学运算,比如通过一些数学公式求解,也可以是符号运 ...

  4. 清风注解-Swift程序设计语言:Point11~15

    目录索引 清风注解-Swift程序设计语言 Point 11. 数值型字面量 代码事例: let decimalInteger = // 十进制的17 let binaryInteger = 0b10 ...

  5. SDN 编程语言 p4(SDN programming language P4)

    行业趋势,SND是未来. P4 是未来. SDN is inevitably, and P4 is inevitably. P4 = Programming Protocol-Independent ...

  6. 在mac上访问自带服务器权限问题

    在开发中,有时候我们需要自己的在电脑上做一些网络相关的测试功能,因此,我们必须在本地上模拟网络环境. 在模拟网络环境中,经常会遇到访问权限的问题.现在我就把自己的解决办法写出来.我用的模拟服务器站点是 ...

  7. 自定义标签体、MVC

    自定义标签 文件以tld结尾,放在webinfo中 标签名 引用类 标签体 继承SimpleTagSupport,复写doTag() getContext(); getjspBody()   invo ...

  8. Android 读取Assets中图片

    bgimg0 = getImageFromAssetsFile("Cat_Blink/cat_blink0000.png"); * * 从Assets中读取图片 */ privat ...

  9. Struts1、Struts2的线程安全问题

    Struts 1.x和Struts 2的Action是不是线程安全的? Struts 1.x在第一次请求某个Action时,会创建这个Action实例.但之后再请求该Action实例时,就用之前创建好 ...

  10. C++/C#结构体转化-二维数组-bytes To Strings

    C++结构体 typedef struct VidyoClientRequestGetWindowsAndDesktops_ { /*! The number of application windo ...