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 ...
随机推荐
- 今天起改用mac的marsedit写博
最近一直使用mac来工作,所以写博也相应改为marsedit. 初步感觉还是不错的,越来越发现mac其实也适合在工作中使用,生活上当然不在话下. 从高富帅的x220t变成屌丝的macbook小白(升级 ...
- 杭电 1272 POJ 1308 小希的迷宫
这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...
- 使用session插件并且实现登录验证
var express = require('express'); var cookieParser = require('cookie-parser'); var bodyParser = requ ...
- 潜在语义分析Latent semantic analysis note(LSA)原理及代码
文章引用:http://blog.sina.com.cn/s/blog_62a9902f0101cjl3.html Latent Semantic Analysis (LSA)也被称为Latent S ...
- English - consist of 和 compose of 的区别
comprise,compose,consist,constitute,include 这一组动词都有"组成,包含"的意思. comprise v.包含,包括,由……组成(整体): ...
- scanf函数和printf函数
C程序实现输出和输入的 主要是printf函数 和 scanf函数,这两个函数是格式输入输出 格式声明由%和格式字符组成 如%d,%f 格式字符: d格式符:用来输出一个有符号的十进制整数 c格式 ...
- html5 input的type属性启动数字输入法
html5 input的type属性启动数字输入法 当文本框只能输入数字是一个很常见的需求,比如电话号码,身份证号,卡号, 数量....等等只允许数字输入,为了更好的用户体验性,直接写出 启动数字 ...
- 输入输出函数库stdio.h
函数名 函数类型与形参类型 函数功能 函数返回值 clearerr void clearerr(fp) FILE * fp; 清除文件指针错误 无 close int close(fp) int fp ...
- JAVA泛型-自动包装机制不能应用于泛型数据的测试
<thinging in java>中指出自动包装机制不能应用于泛型数据,自己写的测试代码如下: package com.xt.thinks15_11_1; import java.uti ...
- HDU 5820 Lights(扫描线+zkw线段树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5820 [题目大意] 在一个大小为50000*50000的矩形中,有n个路灯. 询问是否每一对路灯之 ...