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>
原文地址:https://www.cnblogs.com/wishFreedom/p/3257431.html