XmlManager.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. namespace FileManager
  9. {
  10. public class XmlManager
  11. {
  12. #region 创建Xml文件,并创建根节点和属性
  13. /// <summary>
  14. /// 创建Xml文件,并创建根节点和属性
  15. /// </summary>
  16. /// <param name="xmlfullname">Xml文件的全路径</param>
  17. /// <param name="rootnode">根节点名</param>
  18. /// <param name="list_attributes">根节点的属性键值对</param>
  19. /// <returns></returns>
  20. public Boolean CreateXmlFile(String xmlfullname,String rootnode,List<KeyValuePair<String,String>> list_attributes)
  21. {
  22. XmlDocument xmlDoc = new XmlDocument();
  23. //创建类型声明节点
  24. XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
  25. xmlDoc.AppendChild(node);
  26. //创建根节点
  27. XmlNode root = xmlDoc.CreateElement(rootnode);
  28. xmlDoc.AppendChild(root);
  29. //为根节点增加属性
  30. for (int i = 0; i < list_attributes.Count; i++)
  31. {
  32. XmlNode attribute = xmlDoc.CreateNode(XmlNodeType.Attribute, list_attributes[i].Key, null);
  33. attribute.Value = list_attributes[i].Value;
  34. root.Attributes.SetNamedItem(attribute);
  35. }
  36. try
  37. {
  38. xmlDoc.Save(xmlfullname);
  39. return true;
  40. }
  41. catch (Exception e)
  42. {
  43. //显示错误信息
  44. Console.WriteLine(e.Message);
  45. return false;
  46. }
  47. }
  48. #endregion
  49. #region 创建节点或子节点
  50. /// <summary>
  51. /// 创建节点或子节点
  52. /// </summary>
  53. /// <param name="xmldoc">xml文档</param>
  54. /// <param name="parentnode">父节点</param>
  55. /// <param name="name">节点名</param>
  56. /// <param name="value">节点值</param>
  57. /// <param name="list_attributes">节点属性</param>
  58. public Boolean CreateNode(String xmlfullname, String parentNode, string name, string value, List<KeyValuePair<String, String>> list_attributes)
  59. {
  60. if(!File.Exists(xmlfullname) || name.Trim()=="" || parentNode.Trim()=="")
  61. {
  62. return false;
  63. }
  64. XmlDocument xmlDoc = new XmlDocument();
  65. try
  66. {
  67. xmlDoc.Load(xmlfullname);
  68. XmlNode root = xmlDoc.SelectSingleNode(parentNode);
  69. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  70. //为根节点增加属性
  71. for (int i = 0; i < list_attributes.Count; i++)
  72. {
  73. XmlNode attribute = xmlDoc.CreateNode(XmlNodeType.Attribute, list_attributes[i].Key, null);
  74. attribute.Value = list_attributes[i].Value;
  75. node.Attributes.SetNamedItem(attribute);
  76. }
  77. if (value.Trim() != "")
  78. {
  79. node.InnerText = value;
  80. }
  81. root.AppendChild(node);
  82. xmlDoc.Save(xmlfullname);
  83. return true;
  84. }
  85. catch(Exception e)
  86. {
  87. return false;
  88. }
  89. }
  90. #endregion
  91. }
  92. }