OperateXmlUtil.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Xml;
  7. /// <summary>
  8. /// Class1 的摘要说明
  9. /// </summary>
  10. public class OperateXmlUtil
  11. {
  12. public OperateXmlUtil()
  13. {
  14. //
  15. // TODO: 在此处添加构造函数逻辑
  16. //
  17. }
  18. private static string DefaultPath = "REC";
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. private static XmlDocument _xmlDoc;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. private static XmlDocument xmlDoc
  27. {
  28. get
  29. {
  30. if (_xmlDoc == null)
  31. {
  32. _xmlDoc = new XmlDocument();
  33. }
  34. return _xmlDoc;
  35. }
  36. set
  37. {
  38. _xmlDoc = value;
  39. }
  40. }
  41. /// <summary>
  42. /// 获取指定工号的配置文件节点
  43. /// </summary>
  44. /// <param name="siteName"></param>
  45. /// <param name="siteNo"></param>
  46. /// <param name="empCode"></param>
  47. /// <returns></returns>
  48. public static XmlNode GetSiteNode(string siteName, string siteNo, string empCode)
  49. {
  50. try
  51. {
  52. string configPath = string.Format("{0}\\{1}{2}.xml", DefaultPath, siteName, siteNo);
  53. if (!File.Exists(configPath))
  54. {
  55. if (!Directory.Exists(DefaultPath))
  56. Directory.CreateDirectory(DefaultPath);
  57. CreateConfigFile(configPath);
  58. XmlNode rootNode = GetRootNode(configPath);
  59. InsertNode(configPath, rootNode, "Emp" + empCode, string.Empty);
  60. }
  61. xmlDoc.Load(configPath);
  62. XmlNode configRootNode = GetRootNode(configPath);
  63. XmlNode empNode = configRootNode.SelectSingleNode("Emp" + empCode);
  64. if (empNode == null)
  65. {
  66. InsertNode(configPath, configRootNode, "Emp" + empCode, string.Empty);
  67. }
  68. return empNode;
  69. }
  70. catch
  71. {
  72. return null;
  73. }
  74. }
  75. /// <summary>
  76. /// 新建一个xml配置文件
  77. /// </summary>
  78. /// <param name="filePath"></param>
  79. /// <returns></returns>
  80. public static bool CreateConfigFile(string filePath)
  81. {
  82. try
  83. {
  84. XmlDocument xml = new XmlDocument();
  85. XmlNode rootNode = xml.CreateElement("Root");
  86. xml.AppendChild(rootNode);
  87. xml.Save(filePath);
  88. return true;
  89. }
  90. catch
  91. {
  92. return false;
  93. }
  94. }
  95. /// <summary>
  96. /// 获取配置文件的根节点
  97. /// </summary>
  98. /// <returns></returns>
  99. public static XmlNode GetRootNode(string filePath)
  100. {
  101. try
  102. {
  103. xmlDoc.Load(filePath);
  104. XmlNode empNode = xmlDoc.SelectSingleNode("Root");
  105. return empNode;
  106. }
  107. catch { return null; }
  108. }
  109. public static XmlNode GetContentRootNode(string Content)
  110. {
  111. try
  112. {
  113. xmlDoc.LoadXml(Content);
  114. XmlNode empNode = xmlDoc.SelectSingleNode("root");
  115. return empNode;
  116. }
  117. catch { return null; }
  118. }
  119. /// <summary>
  120. /// 获取配置文件
  121. /// </summary>
  122. /// <param name="siteName"></param>
  123. /// <param name="siteNo"></param>
  124. /// <returns></returns>
  125. public static string GetConfigPath(string siteName, string siteNo)
  126. {
  127. string configPath = string.Format("{0}\\{1}{2}.xml", DefaultPath, siteName, siteNo);
  128. return configPath;
  129. }
  130. /// <summary>
  131. /// 插入一个指定名称的节点
  132. /// </summary>
  133. /// <param name="configPath"></param>
  134. /// <param name="ParentNode"></param>
  135. /// <param name="nodeName"></param>
  136. /// <returns></returns>
  137. public static Boolean InsertNode(string configPath, XmlNode ParentNode, string nodeName, string value)
  138. {
  139. try
  140. {
  141. XmlDocument doc = ParentNode.OwnerDocument;
  142. XmlNode childNode = ParentNode.SelectSingleNode(nodeName);
  143. if (childNode != null)
  144. {
  145. childNode.InnerText = value;
  146. doc.Save(configPath);
  147. return true;
  148. }
  149. XmlNode newNode = doc.CreateElement(nodeName);
  150. newNode.InnerText = value;
  151. ParentNode.AppendChild(newNode);
  152. doc.Save(configPath);
  153. return true;
  154. }
  155. catch
  156. {
  157. return false;
  158. }
  159. }
  160. /// <summary>
  161. /// 给指定节点插入指定值
  162. /// </summary>
  163. /// <param name="configPath"></param>
  164. /// <param name="node"></param>
  165. /// <param name="value"></param>
  166. /// <returns></returns>
  167. public static Boolean SetNodeValue(string configPath, XmlNode node, string value)
  168. {
  169. try
  170. {
  171. xmlDoc.Load(configPath);
  172. node.InnerText = value;
  173. xmlDoc.Save(configPath);
  174. return true;
  175. }
  176. catch
  177. {
  178. return false;
  179. }
  180. }
  181. /// <summary>
  182. /// 给指定节点插入指定属性
  183. /// </summary>
  184. /// <param name="configPath"></param>
  185. /// <param name="node"></param>
  186. /// <param name="attributeName"></param>
  187. /// <param name="attributeValue"></param>
  188. /// <returns></returns>
  189. public static Boolean InsertNodeAttribute(string configPath, XmlNode node, string attributeName, string attributeValue)
  190. {
  191. try
  192. {
  193. xmlDoc.Load(configPath);
  194. XmlElement element = (XmlElement)node;
  195. element.SetAttribute(attributeName, attributeValue);
  196. xmlDoc.Save(configPath);
  197. return true;
  198. }
  199. catch
  200. {
  201. return false;
  202. }
  203. }
  204. /// <summary>
  205. /// 获取指定名称的子节点
  206. /// </summary>
  207. /// <param name="parentNode"></param>
  208. /// <param name="childNodeName"></param>
  209. /// <returns></returns>
  210. public static XmlNode GetChildNode(string configPath, XmlNode parentNode, string childNodeName)
  211. {
  212. try
  213. {
  214. XmlNode childNode = parentNode.SelectSingleNode(childNodeName);
  215. if (childNode == null)
  216. {
  217. InsertNode(configPath, parentNode, childNodeName, string.Empty);
  218. childNode = parentNode.SelectSingleNode(childNodeName);
  219. }
  220. return childNode;
  221. }
  222. catch
  223. {
  224. return null;
  225. }
  226. }
  227. }