XML读写文件辅助类
/// <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读写文件辅助类的更多相关文章
- xml读写文件实例
在某个通讯中需要向服务器发送请求xml,格式例子如下: <?xml version="1.0" encoding="UTF-8"?> <ROO ...
- OpenCV FileStorage类读写XML/YML文件
本文转自:http://www.cnblogs.com/summerRQ/articles/2524560.html 在OpenCV程序中,需要保存中间结果的时候常常会使用.xml / .yml文件, ...
- XML结构文件的读写
附件:http://files.cnblogs.com/xe2011/XML_Writer_And_Read.rar 下面这段代码实现了以下功能 数据保存 textBox1的文本,textBox2的文 ...
- OpenCV教程(42) xml/yaml文件的读写
参考资料: http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html #include "opencv2/openc ...
- Inno Setup 如何读写文件
软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...
- Android用路径api在内部存储读写文件
复制并修改原有项目 复制之前创建的项目CC+CV操作 需要改动的地方: * 项目名字 * 应用包名 * R文件重新导包 接着修改件/AndroidManifest.xml中的包名:package=&q ...
- Ibatis学习总结4--SQL Map XML 映射文件扩展
SQL Map XML 映射文件除了上文提到的属性还有一些其他重要的属性,下文将详细介绍这些属性. 缓存 Mapped Statement 结果集 通过在查询 statement 中指定 cacheM ...
- android 学习随笔二(读写文件)
在android读写文件 RAM:运行内存,相当于电脑的内存 ROM:内部存储空间,相当电脑硬盘,android手机必须有的 SD卡:外部存储空间,相当电脑的移动硬盘,不是必须的.手机如果内置16G存 ...
- 网站的配置文件XML读写
网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考. 读操作: XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettin ...
随机推荐
- 深入Blocks分析
1.简介 从iOS4开始,苹果引入了这个C语言的扩充功能"Blocks",在一些特定的场景下也是一把利刃.我前面一篇博客中初步介绍了Blocks这个东西,主要是语法的介绍(< ...
- How Many Equations Can You Find(dfs)
How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HP DL360 G7通过iLO部署系统
HPDL360 G7通过iLO部署系统 HP DL360 G7是没有光驱的服务器,可使用USB外置光驱.PXE网络安装.ILO方式的安装操作系统 一.HP iLO 简介 iLO 是一组芯片,内部是vx ...
- AIDL 发生异常的原因 Android java.lang.SecurityException: Binder invocation to an incorrect interface
我建立了两个project.一个是activity 的 ,一个是service 的. 在进行两个project通信时,应该有以下几点注意: 1.在activity project中引入service ...
- C#实现多态之一抽象
1. 抽象类.抽象方法.抽象属性的特点 (1) 关键字:abstract (2) 抽象类只能是其他类的基类 (3) 抽象成员必须存在于抽象类中,但抽象类可以没有抽象成员, ...
- STL模板_容器概念
一.STL(Standard Template Library,标准模板库)概述1.容器:基于泛型的数据结构.2.算法:基于泛型的常用算法.3.迭代器:以泛型的方式访问容器中的元素,是泛型的算法可以应 ...
- LineCalc,一个基于Lex&Yacc的简单行计算工具
LineCalc是基于Lex&Yacc的一个简单的行计算工具,支持常见的运算符和部分POSIX中定义于math.h中的数学函数:同时,LineCalc还提供了一个简单的错误处理模块,能检测公式 ...
- Android常用工具类封装---SharedPreferencesUtil
SharedPreferences常用于保存一些简单的数据,如记录用户操作的配置等,使用简单. public class SharedPreferencesUtil { // ...
- 定义一个runtime的Annotation
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(value ...
- 用Cython加速Python程序以及包装C程序简单测试
用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...