using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Xml; /// /// Class1 的摘要说明 /// public class OperateXmlUtil { public OperateXmlUtil() { // // TODO: 在此处添加构造函数逻辑 // } private static string DefaultPath = "REC"; /// /// /// private static XmlDocument _xmlDoc; /// /// /// private static XmlDocument xmlDoc { get { if (_xmlDoc == null) { _xmlDoc = new XmlDocument(); } return _xmlDoc; } set { _xmlDoc = value; } } /// /// 获取指定工号的配置文件节点 /// /// /// /// /// 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; } } /// /// 新建一个xml配置文件 /// /// /// 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; } } /// /// 获取配置文件的根节点 /// /// 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; } } /// /// 获取配置文件 /// /// /// /// public static string GetConfigPath(string siteName, string siteNo) { string configPath = string.Format("{0}\\{1}{2}.xml", DefaultPath, siteName, siteNo); return configPath; } /// /// 插入一个指定名称的节点 /// /// /// /// /// 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; } } /// /// 给指定节点插入指定值 /// /// /// /// /// 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; } } /// /// 给指定节点插入指定属性 /// /// /// /// /// /// 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; } } /// /// 获取指定名称的子节点 /// /// /// /// 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; } } }