简单XML操作类

前端之家收集整理的这篇文章主要介绍了简单XML操作类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /// <summary>
  2. /// 返回XMl文件指定元素的指定属性
  3. /// </summary>
  4. /// <param name="xmlElement">指定元素</param>
  5. /// <param name="xmlAttribute">指定属性</param>
  6. /// <returns></returns>
  7. public static bool getXmlValue(string key,out string value)
  8. {
  9. XmlDocument xmlDoc = new XmlDocument();
  10. xmlDoc.Load(xmlName);
  11.  
  12. XmlNode xmlNode = xmlDoc.SelectSingleNode("root");
  13.  
  14. XmlNodeList xmlList = xmlNode.SelectNodes("data");
  15.  
  16. foreach (XmlElement temp in xmlList)
  17. {
  18. if (temp.GetAttribute("key").ToString() == key)
  19. {
  20. value = temp.GetAttribute("value");
  21. return true;
  22. }
  23. }
  24.  
  25. value = "";
  26. return false;
  27. }
  28.  
  29. /// <summary>
  30. /// 设置XMl文件指定元素的指定属性的值
  31. /// </summary>
  32. /// <param name="xmlElement">指定元素</param>
  33. /// <param name="xmlAttribute">指定属性</param>
  34. /// <param name="xmlValue">指定值</param>
  35. public static bool setXmlValue(string key,string value)
  36. {
  37. XmlDocument xmlDoc = new XmlDocument();
  38. xmlDoc.Load(xmlName);
  39.  
  40. XmlNode xmlNode = xmlDoc.SelectSingleNode("root");
  41.  
  42. XmlNodeList xmlList = xmlNode.SelectNodes("data");
  43.  
  44. foreach (XmlElement temp in xmlList)
  45. {
  46. if (temp.GetAttribute("key").ToString() == key)
  47. {
  48. temp.SetAttribute("value",value);
  49. xmlDoc.Save(xmlName);
  50. return true;
  51. }
  52. }
  53.  
  54. return false;
  55.  
  56. }
  57.  
  58. /// <summary>
  59. /// 增加一个属性存储
  60. /// </summary>
  61. /// <param name="xmlElement"></param>
  62. /// <param name="xmlAttribute"></param>
  63. /// <param name="xmlValue"></param>
  64. public static bool addValue(string xmlElement,string key,string value)
  65. {
  66. //XmlDocument xmlDoc = new XmlDocument();
  67. //xmlDoc.Load(xmlName);
  68.  
  69. //XmlElement xmlNode = xmlDoc.CreateElement(xmlElement);
  70. //xmlNode.SetAttribute("key",key);
  71. //xmlNode.SetAttribute("value",value);
  72.  
  73. //XmlNode xml = xmlDoc.DocumentElement.PrependChild(xmlNode);
  74. //xmlDoc.Save(xmlName);
  75.  
  76. bool isExist = false;
  77.  
  78. XmlDocument xmlDoc = new XmlDocument();
  79. xmlDoc.Load(xmlName);
  80.  
  81. XmlNode xmlNode1 = xmlDoc.SelectSingleNode("root");
  82.  
  83. XmlNodeList xmlList = xmlNode1.SelectNodes("data");
  84.  
  85. foreach (XmlElement temp in xmlList)
  86. {
  87. if (temp.GetAttribute("key").ToString() == key)
  88. {
  89. isExist = true;
  90. }
  91. }
  92.  
  93. if (!isExist)
  94. {
  95. XmlElement xmlNode = xmlDoc.CreateElement(xmlElement);
  96. xmlNode.SetAttribute("key",key);
  97. xmlNode.SetAttribute("value",value);
  98.  
  99. XmlNode xml = xmlDoc.DocumentElement.PrependChild(xmlNode);
  100. xmlDoc.Save(xmlName);
  101. return true;
  102. }
  103.  
  104. return false;
  105. }
  106.  
  107. /// <summary>
  108. /// 遍历方法
  109. /// </summary>
  110. /// <param name="orglist"></param>
  111. /// <param name="xmlElement"></param>
  112. /// <returns></returns>
  113. public static IDictionary<string,string> Search(List<string> orglist)
  114. {
  115. IDictionary<string,string> dic = new Dictionary<string,string>();
  116.  
  117. XmlDocument xmlDoc = new XmlDocument();
  118. xmlDoc.Load(xmlName);
  119.  
  120. XmlNode xmlNode1 = xmlDoc.SelectSingleNode("root");
  121.  
  122. XmlNodeList xmlList = xmlNode1.SelectNodes("data");
  123.  
  124. foreach (XmlElement temp in xmlList)
  125. {
  126. foreach (string tempOrg in orglist)
  127. {
  128. if (temp.GetAttribute("key").ToString() == tempOrg)
  129. {
  130. dic.Add(tempOrg,temp.GetAttribute("value").ToString());
  131. }
  132. }
  133. }
  134.  
  135. return dic;
  136. }

猜你在找的XML相关文章