/// <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. Android之CookieStore的持久化

    CookieStore是一个对象,有的服务端 ,比如.net,保持登录状态不是用httpclient.addHeader(“cookie”,SessionId),而是用httppost.setCook ...

  2. ios webview 加载含有中文的URL网页显示白屏

    1. ios中的webview加载的URL不可以含有中文,解决办法说将中文字符转码, 如下: - (NSString *)URLEncodeString { NSCharacterSet *set = ...

  3. 城市平乱(Bellman)

    城市平乱 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...

  4. 7. Reverse Integer

    1. 问题描述 Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321 cl ...

  5. SQL知识三(Day 27)

    大家好,好几天都没写博客了.因为自己的一些原因,落下了很多.今天没有学什么新的知识,自己就把以前落下的好好看了一下.好了,今天就先总结一下SQL剩下的一些知识吧. 主要学的知识有:循环语句(case语 ...

  6. iOS开发之C语言函数库

    在iOS开发中不可避免的需要使用数学函数进行计算,我们可以使用C标准库头文件中定义的数学常量宏及数学函数来进行基本的数学计算操作,这些C函数都可以在微软的MSDNAPI库中找到.(https://ms ...

  7. 理解ROS的参数

    记住每次操作之前都要在一个单独的终端中运行ros的核心. roscore rosparam命令允许你在ROS的参数服务器上操作和存储数据,参数服务器可以存储整数,浮点数,布尔类型,字典,列表.ROS使 ...

  8. adb wifi连接手机

    1. 默认情况下,ADB是通过USB来进行连接的. 不需要USB线,直接在android设备上安装一个超级终端,在终端里运行以下代码即可: su setprop service.adb.tcp.por ...

  9. web server服务器

    使用最多的 web server服务器软件有两个:微软的信息服务器(iis),和Apache. 通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序 ...

  10. CSS完美兼容IE6/IE7/IE8/IE9/IE10的通用方法

    关于CSS对各个浏览器兼容已经是老生常谈的问题了, 网络上的教程遍地都是.以下内容没有太多新颖, 纯属个人总结, 希望能对初学者有一定的帮助. 一.CSS HACK 以下两种方法几乎能解决现今所有HA ...