|
@@ -0,0 +1,237 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Web;
|
|
|
+using System.Xml;
|
|
|
+
|
|
|
+/// <summary>
|
|
|
+/// Class1 的摘要说明
|
|
|
+/// </summary>
|
|
|
+public class OperateXmlUtil
|
|
|
+{
|
|
|
+ public OperateXmlUtil()
|
|
|
+ {
|
|
|
+ //
|
|
|
+ // TODO: 在此处添加构造函数逻辑
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string DefaultPath = "REC";
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ private static XmlDocument _xmlDoc;
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ private static XmlDocument xmlDoc
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (_xmlDoc == null)
|
|
|
+ {
|
|
|
+ _xmlDoc = new XmlDocument();
|
|
|
+ }
|
|
|
+ return _xmlDoc;
|
|
|
+ }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ _xmlDoc = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取指定工号的配置文件节点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="siteName"></param>
|
|
|
+ /// <param name="siteNo"></param>
|
|
|
+ /// <param name="empCode"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static XmlNode GetSiteNode(string siteName, string siteNo, string empCode)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string configPath = string.Format("{0}\\{1}{2}.xml", DefaultPath, siteName, siteNo);
|
|
|
+ if (!File.Exists(configPath))
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(DefaultPath))
|
|
|
+ Directory.CreateDirectory(DefaultPath);
|
|
|
+ CreateConfigFile(configPath);
|
|
|
+ XmlNode rootNode = GetRootNode(configPath);
|
|
|
+ InsertNode(configPath, rootNode, "Emp" + empCode, string.Empty);
|
|
|
+ }
|
|
|
+ xmlDoc.Load(configPath);
|
|
|
+ XmlNode configRootNode = GetRootNode(configPath);
|
|
|
+ XmlNode empNode = configRootNode.SelectSingleNode("Emp" + empCode);
|
|
|
+ if (empNode == null)
|
|
|
+ {
|
|
|
+ InsertNode(configPath, configRootNode, "Emp" + empCode, string.Empty);
|
|
|
+ }
|
|
|
+ return empNode;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 新建一个xml配置文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="filePath"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static bool CreateConfigFile(string filePath)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ XmlDocument xml = new XmlDocument();
|
|
|
+ XmlNode rootNode = xml.CreateElement("Root");
|
|
|
+ xml.AppendChild(rootNode);
|
|
|
+ xml.Save(filePath);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取配置文件的根节点
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static XmlNode GetRootNode(string filePath)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ xmlDoc.Load(filePath);
|
|
|
+ XmlNode empNode = xmlDoc.SelectSingleNode("Root");
|
|
|
+ return empNode;
|
|
|
+ }
|
|
|
+ catch { return null; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static XmlNode GetContentRootNode(string Content)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ xmlDoc.LoadXml(Content);
|
|
|
+ XmlNode empNode = xmlDoc.SelectSingleNode("root");
|
|
|
+ return empNode;
|
|
|
+ }
|
|
|
+ catch { return null; }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取配置文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="siteName"></param>
|
|
|
+ /// <param name="siteNo"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string GetConfigPath(string siteName, string siteNo)
|
|
|
+ {
|
|
|
+ string configPath = string.Format("{0}\\{1}{2}.xml", DefaultPath, siteName, siteNo);
|
|
|
+ return configPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 插入一个指定名称的节点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="configPath"></param>
|
|
|
+ /// <param name="ParentNode"></param>
|
|
|
+ /// <param name="nodeName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Boolean InsertNode(string configPath, XmlNode ParentNode, string nodeName, string value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ XmlDocument doc = ParentNode.OwnerDocument;
|
|
|
+ XmlNode childNode = ParentNode.SelectSingleNode(nodeName);
|
|
|
+ if (childNode != null)
|
|
|
+ {
|
|
|
+ childNode.InnerText = value;
|
|
|
+ doc.Save(configPath);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ XmlNode newNode = doc.CreateElement(nodeName);
|
|
|
+ newNode.InnerText = value;
|
|
|
+ ParentNode.AppendChild(newNode);
|
|
|
+ doc.Save(configPath);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 给指定节点插入指定值
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="configPath"></param>
|
|
|
+ /// <param name="node"></param>
|
|
|
+ /// <param name="value"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Boolean SetNodeValue(string configPath, XmlNode node, string value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ xmlDoc.Load(configPath);
|
|
|
+ node.InnerText = value;
|
|
|
+ xmlDoc.Save(configPath);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 给指定节点插入指定属性
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="configPath"></param>
|
|
|
+ /// <param name="node"></param>
|
|
|
+ /// <param name="attributeName"></param>
|
|
|
+ /// <param name="attributeValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Boolean InsertNodeAttribute(string configPath, XmlNode node, string attributeName, string attributeValue)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ xmlDoc.Load(configPath);
|
|
|
+ XmlElement element = (XmlElement)node;
|
|
|
+ element.SetAttribute(attributeName, attributeValue);
|
|
|
+ xmlDoc.Save(configPath);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取指定名称的子节点
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="parentNode"></param>
|
|
|
+ /// <param name="childNodeName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static XmlNode GetChildNode(string configPath, XmlNode parentNode, string childNodeName)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ XmlNode childNode = parentNode.SelectSingleNode(childNodeName);
|
|
|
+ if (childNode == null)
|
|
|
+ {
|
|
|
+ InsertNode(configPath, parentNode, childNodeName, string.Empty);
|
|
|
+ childNode = parentNode.SelectSingleNode(childNodeName);
|
|
|
+ }
|
|
|
+ return childNode;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|