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 ...
随机推荐
- Wet Shark and Flowers(思维)
C. Wet Shark and Flowers time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- netbeans字体与颜色配置模板相关网站
NetBeans Themes -Color Schemes of the NetBeans IDE NetBeans ThemeBuilder
- Ruby on Rails开发Web应用的基本概念
Web应用架构 C/S架构 Web应用从最初就採用C/S架构.Server负责监听client请求,提供资源,Client向server发起请求并渲染页面.两者通过TCP/IP协议栈之上的HTTP协议 ...
- 多个Activity之间的切换与数据交互
总结 两个activity之间切换我概括的分为两步: 1. 代码实现切换操作.2.配置中声明另外一个acitivity! 1. 代码实现切换操作 显示定义一个intent 对象,Intent 这个类的 ...
- SQLite 终端相关命令
SQLite ALL Last login: Fri Dec 5 09:52:08 on ttys002 BeSilent:~ qianfeng$ sqlite3 data.db SQLite ve ...
- 深入理解java虚拟机系列二——垃圾收集算法
在主流的商用程序语言中大多都是用根搜索算法(GC Roots Tracing)判断对象是否存活,比如java,c#等.当从GC Roots到某个对象不可达,则证明此对象是不可用的,将要被回收. 商业虚 ...
- linux 定时执行任务
测试可以了,做个笔记 系统是centos 6.3 1,直接命令 crontab -e 编辑文件,里面写时间和你想要执行的命令. 例子 */1 * * * * sh /home/guanliyang/t ...
- Html 小插件5 百度搜索代码2
网页添加百度搜索框代码大全 ★ 用法:在下面选择合适的样式,复制代码到网页中相应位置粘贴即可. ★ 样式一(200×30)代码: <iframe id="baiduframe" ...
- 个人信用卡管理 - iOS UI原型
- HDU 3374 String Problem (KMP+最小最大表示)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3374 [题目大意] 给出一个字符串,求出最小和最大表示是从哪一位开始的,并且输出数量. [题解] ...