【CS】客户端更新(二)——生成更新配置文件程序介绍

前端之家收集整理的这篇文章主要介绍了【CS】客户端更新(二)——生成更新配置文件程序介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、前言

上一篇博客中,小编向大家介绍了【CS】客户端更新(一)——更新程序文件方式,更新的内容都是写在配置文件中,自然而然我们不可能手动去写配置文件,在后期维护非常的不方便,下面小编就结合上一篇博客,把更新的配置文件的操作展示一下。

二、配置文件结构分析

不同的程序有不同的配置文件,小编在项目中使用的配置文件的类型是*.xml文件。xml文件的最大的特点就是可以携带数据,使用方便。

下面是小编使用的配置文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AutoUpdater>
  3. <Updater>
  4. <Url>ftp://73.16.18.144/</Url>
  5. </Updater>
  6. <Application applicationId="ItemSoft">
  7. <EntryPoint>ItemSoft</EntryPoint>
  8. <Location>.</Location>
  9. </Application>
  10. <Files>
  11. <File Ver="fa6d7e00-0c64-4ccd-b1c2-c7289268e5a6" Name="123.mp4" />
  12. <File Ver="d4b52955-0712-4274-ac6b-64d7415ccbf3" Name="DESDecder.exe" />
  13. <File Ver="26e31dd8-85e1-4762-8101-dc6715de7d01" Name="fdsfs.wmv" />
  14. <File Ver="892fc9cc-b4fd-42b1-a235-09bfa8f87e21" Name="test/4454.mp3" />
  15. </Files>
  16. <Update>
  17. <Soft Name="BlogWriter">
  18. <Verson>b9c69d80-203d-4280-8ffe-1e4ae6ca621c</Verson>
  19. </Soft>
  20. </Update>
  21. </AutoUpdater>

在每一个xml配置文件中都有文件

三、程序代码生成介绍

程序如下:输入地址,点击生成,就可以得到生成的xml文件内容


生成xml文件

  1. #region 创建xml文件-固定的格式-王雷-2017年4月24日16:24:35
  2. /// <summary>
  3. /// 创建xml文件-固定的格式-王雷-2017年4月24日16:24:35
  4. /// </summary>
  5. /// <param name="url">webservice的地址</param>
  6. void CreateXml(string url)
  7. {
  8. //创建文档对象
  9. XmlDocument doc = new XmlDocument();
  10. //创建根节点
  11. XmlElement root = doc.CreateElement("AutoUpdater");
  12. //头声明
  13. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0","utf-8",null);
  14. doc.AppendChild(xmldecl);
  15. DirectoryInfo dicInfo = new DirectoryInfo(currentDirectory);
  16.  
  17. //Updater
  18. XmlElement body1 = doc.CreateElement("Updater");
  19. Tool.AddChildNode(body1,doc,"Url",url);
  20. root.AppendChild(body1);
  21.  
  22. //Application
  23. XmlElement body2 = doc.CreateElement("Application");
  24. Tool.AddEleAttr(body2,"applicationId","ItemSoft");
  25. Tool.AddChildNode(body2,"EntryPoint","Location",".");
  26. root.AppendChild(body2);
  27.  
  28. //Files
  29. XmlElement body3 = doc.CreateElement("Files");
  30. //调用递归方法组装xml文件
  31. PopuAllDirectory(doc,body3,dicInfo);
  32. root.AppendChild(body3);
  33.  
  34. //Update
  35. XmlElement body4 = doc.CreateElement("Update");
  36. root.AppendChild(body4);
  37. XmlElement body5 = doc.CreateElement("Soft");
  38. Tool.AddEleAttr(body5,"Name","BlogWriter");
  39. Tool.AddChildNode(body5,"Verson",Guid.NewGuid().ToString());
  40. body4.AppendChild(body5);
  41.  
  42. //追加节点
  43. doc.AppendChild(root);
  44. //保存文档
  45. doc.Save(serverXmlName);
  46. }
  47. #endregion

在其中用到了两个方法

向XML元素添加子节点:

  1. #region XML元素添加子节点-王雷-2017年4月24日16:26:37
  2. /// <summary>
  3. /// XML元素添加子节点-王雷-2017年4月24日16:26:37
  4. /// </summary>
  5. public static void AddChildNode(this XmlElement src,XmlDocument doc,string name,string innerText)
  6. {
  7. XmlElement elem = doc.CreateElement(name);
  8. elem.InnerText = innerText;
  9. src.AppendChild(elem);
  10. }
  11. #endregion

向XML元素添加属性

  1. #region XML元素添加属性-王雷-2017年4月24日16:27:46
  2. /// <summary>
  3. /// XML元素添加属性-王雷-2017年4月24日16:27:46
  4. /// </summary>
  5. public static void AddEleAttr(this XmlElement src,string value)
  6. {
  7. XmlAttribute attr = doc.CreateAttribute(name);
  8. attr.Value = value;
  9. src.Attributes.Append(attr);
  10. }
  11. #endregion

递归的方式组装xml文件方法

  1. //递归组装xml文件方法
  2. private void PopuAllDirectory(XmlDocument doc,XmlElement root,DirectoryInfo dicInfo)
  3. {
  4. foreach (FileInfo f in dicInfo.GetFiles())
  5. {
  6. //排除当前目录中生成xml文件的工具文件
  7. if (f.Name != "CreateXmlTools.exe" && f.Name != "AutoupdateService.xml" && f.Name != "AutoUpdate.exe" && f.Name.LastIndexOf(".pdb") == -1 && f.Name != "UpdateList.xml" && f.Name.LastIndexOf(".vshost.exe") == -1 && f.Name.LastIndexOf(".vshost.exe.manifest") == -1)
  8. {
  9. string path = dicInfo.FullName.Replace(currentDirectory,"").Replace("\\","/");
  10. //path = dicInfo.FullName.Replace(currentDirectory,"").Replace(@"\","/");
  11. string folderPath=string.Empty;
  12. if (path != string.Empty)
  13. {
  14. folderPath = path.TrimStart('/') + "/";
  15. }
  16. XmlElement child = doc.CreateElement("File");
  17. child.SetAttribute("Ver",Guid.NewGuid().ToString());
  18. child.SetAttribute("Name",folderPath + f.Name);
  19. root.AppendChild(child);
  20. }
  21. }
  22.  
  23. foreach (DirectoryInfo di in dicInfo.GetDirectories())
  24. PopuAllDirectory(doc,root,di);
  25. }

读取xml文件

  1. private void ReadXml()
  2. {
  3. string path="UpdateList.xml";
  4. rtbXml.ReadOnly = true;
  5. if (File.Exists(path))
  6. {
  7. rtbXml.Text = File.ReadAllText(path);
  8. }
  9. }

四、小结

通过这次的接触,自己动手拼xml文件,把节点和属性一个一个的拼接上,然后就可以得到自己需要的配置文件,不用手写了,这样可以保证效率提高,出错率降低了。很不错的方式。加油!

博客不容易,官人,打个赏呗~~

福利:软件更新生成配置文件源码

猜你在找的XML相关文章