XmlManager.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.IO;
  8. using System.Reflection;
  9. namespace FileManager
  10. {
  11. public class XmlManager
  12. {
  13. #region 创建Xml文件,并创建根节点和属性
  14. /// <summary>
  15. /// 创建Xml文件,并创建根节点和属性
  16. /// </summary>
  17. /// <param name="xmlfullname">Xml文件的全路径</param>
  18. /// <param name="rootnode">根节点名</param>
  19. /// <param name="list_attributes">根节点的属性键值对</param>
  20. /// <returns></returns>
  21. public static Boolean CreateXmlFile(String xmlfullname)
  22. {
  23. XmlDocument xmlDoc = new XmlDocument();
  24. //创建类型声明节点
  25. XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
  26. xmlDoc.AppendChild(node);
  27. //创建根节点
  28. XmlNode root = xmlDoc.CreateElement("XMLData");
  29. xmlDoc.AppendChild(root);
  30. try
  31. {
  32. xmlDoc.Save(xmlfullname);
  33. return true;
  34. }
  35. catch (Exception e)
  36. {
  37. //显示错误信息
  38. Console.WriteLine(e.Message);
  39. return false;
  40. }
  41. }
  42. #endregion
  43. #region 获取Xml文件中节点内的信息
  44. /// <summary>
  45. /// 获取Xml文件中节点内的信息
  46. /// </summary>
  47. /// <param name="xmlfullname">Xml文件的全路径</param>
  48. /// <param name="measureCode">线程编号</param>
  49. /// <returns></returns>
  50. public static XmlNodeList GetXmlMeasureFlowNodeInfo(String xmlfullname, string measureCode)
  51. {
  52. try
  53. {
  54. XmlDocument xmlDoc = new XmlDocument();
  55. //加载XML文件
  56. xmlDoc.Load(xmlfullname);
  57. //获得文档中的根节点。
  58. XmlElement xmlElement = xmlDoc.DocumentElement;
  59. XmlNodeList nodeList = xmlElement.ChildNodes;
  60. XmlNodeList nodeChildList = null;
  61. foreach (XmlNode item in nodeList)
  62. {
  63. if (item.Attributes["Code"].Value == measureCode)
  64. {
  65. //获取当前节点下的所有信息
  66. nodeChildList = item.ChildNodes;
  67. break;
  68. }
  69. }
  70. return nodeChildList;
  71. }
  72. catch (Exception ex)
  73. {
  74. LogManager.LogError(ex.ToString());
  75. return null;
  76. }
  77. }
  78. #endregion
  79. }
  80. }