using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; using System.IO; namespace 人事管理系統.Helper { public class xmlHelper { #region 字段 /// <summary> /// xml文件物理路徑 /// </summary> private string _FilePath = string.Empty; /// <summary> /// xml文檔 /// </summary> private XmlDocument _xml; /// <summary> /// xml文檔根節點 /// </summary> private XmlElement _element; #endregion public xmlHelper() { // } /// <summary> /// 給xml文檔路徑賦值 /// </summary> /// <param name="xmlFilePath"></param> public xmlHelper(string xmlFilePath) { _FilePath = xmlFilePath; } /// <summary> /// 獲取指定路徑節點 /// </summary> /// <param name="xPath"></param> /// <returns></returns> public static XmlNode GetXmlNode(string xmlFileName, string xPath) { XmlDocument xmldocument = new XmlDocument(); //加載xml文檔 xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); return xmlnode; } catch { return null; } } /// <summary> /// 獲取指定路徑節點下孩子節點列表 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <returns></returns> public static XmlNodeList GetXmlNodeList(string xmlFileName, string xPath) { XmlDocument xmldocument = new XmlDocument(); //加載xml文檔 xmldocument.Load(xmlFileName); try { XmlNodeList xmlnodelist = xmldocument.SelectNodes(xPath); return xmlnodelist; } catch { return null; } } /// <summary> /// 獲取指定路徑節點的屬性與指定屬性名匹配 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath">要匹配的XPath表達式(例如:"//節點名//子節點名</param> /// <param name="attributeName">指定的屬性名稱</param> /// <returns></returns> public static XmlAttribute GetXmlAttribute(string xmlFileName, string xPath,string attributeName) { XmlAttribute xmlattribute=null; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { if (xmlnode.Attributes.Count > 0) { xmlattribute = xmlnode.Attributes[attributeName]; } } } catch (Exception err) { throw err; } return xmlattribute; } /// <summary> /// 獲取指定節點的屬性集合 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <returns></returns> public static XmlAttributeCollection GetNodeAttributes(string xmlFileName, string xPath) { XmlAttributeCollection xmlattributes = null; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { if (xmlnode.Attributes.Count > 0) { xmlattributes = xmlnode.Attributes; } } } catch (Exception err) { throw err; } return xmlattributes; } /// <summary> /// 更新指定節點的某一屬性設定其屬性值value /// </summary> /// <param name="xmlFileName">xml文檔路徑</param> /// <param name="xPath"></param> /// <param name="attributeOldeName">舊屬性名稱</param> /// <param name="attributeNewName">新屬性名稱</param> /// <param name="value">屬性值</param> /// <returns>成功返回true,失敗返回false</returns> public static bool UpdateAttribute(string xmlFileName, string xPath, string attributeName, string value) { bool isSuccess = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { foreach (XmlAttribute attribute in xmlnode.Attributes) { if (attribute.Name.ToString().ToLower() == attributeName.ToLower()) { isSuccess = true; attribute.Value = value; xmldocument.Save(xmlFileName); break; } } } } catch(Exception err) { throw err; } return isSuccess; } /// <summary> /// 刪除指定節點的所有屬性 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <returns>成功返回true,失敗返回false</returns> public static bool DeleteAttributes(string xmlFileName, string xPath) { bool isSuccess = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { if (xmlnode.Attributes.Count > 0) { xmlnode.Attributes.RemoveAll(); xmldocument.Save(xmlFileName); isSuccess = true; } } } catch (Exception err) { throw err; } return isSuccess; } /// <summary> /// 刪除匹配屬性名稱的指定節點的屬性 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <param name="attributeName"></param> /// <returns></returns> public static bool DeleteOneAttribute(string xmlFileName, string xPath, string attributeName) { bool isSuccess = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); XmlAttribute xmlAttribute = null; try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { if (xmlnode.Attributes.Count > 0) { foreach (XmlAttribute attribute in xmlnode.Attributes) { if (attribute.Name.ToLower() == attributeName.ToLower()) { xmlAttribute = attribute; break; } } } if (xmlAttribute != null) { xmlnode.Attributes.Remove(xmlAttribute); xmldocument.Save(xmlFileName); isSuccess = true; } } } catch (Exception err) { throw err; } return isSuccess; } /// <summary> /// 創建指定節點的屬性,如果屬性存在則不創建 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <param name="attributeName"></param> /// <param name="value"></param> /// <returns></returns> public static bool AddAttribute(string xmlFileName, string xPath,string attributeName,string value) { bool isSuccess = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { if (xmlnode.Attributes.Count > 0)//遍歷判斷有無此屬性 { foreach (XmlAttribute attribute in xmlnode.Attributes) { if (attribute.Name.ToLower() == attributeName.ToLower()) { //有則不改,直接返回true; return true; } } } XmlAttribute xmlAttribute = xmldocument.CreateAttribute(attributeName); xmlAttribute.Value = value; xmlnode.Attributes.Append(xmlAttribute); xmldocument.Save(xmlFileName); isSuccess = true; } } catch (Exception err) { throw err; } return isSuccess; } /// <summary> /// 為某一指定路徑節點下添加新的節點,如果該節點存在,則不添加 /// </summary> /// <param name="xmlFileName">xml文檔路徑</param> /// <param name="xPath">需要添加節點的路徑</param> /// <param name="nodeName">節點名稱</param> /// <param name="innerText">節點文本值</param> /// <returns>成功返回true,存在返回false</returns> public static bool AddNode(string xmlFileName, string xPath, string nodeName, string innerText) { bool isSuccess = false; bool isExisitNode = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { isExisitNode = true; } if (!isExisitNode) { XmlElement subElement = xmldocument.CreateElement(nodeName); subElement.InnerText = innerText; xmlnode.AppendChild(subElement); isSuccess = true; xmldocument.Save(xmlFileName); } } catch (Exception err) { throw err; } return isSuccess; } /// <summary> /// 查找指定的節點,更新其節點值 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <param name="nodeName"></param> /// <param name="innerText"></param> /// <returns></returns> public static bool UpdateNode(string xmlFileName, string xPath, string nodeName, string innerText) { bool isSuccess = false; bool isExisitNode = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); try { if (xmlnode != null) { isExisitNode = true; } if (!isExisitNode) { xmlnode.InnerText = innerText; isSuccess = true; xmldocument.Save(xmlFileName); } } catch (Exception err) { throw err; } return isSuccess; } /// <summary> /// 刪除指定節點名稱為nodeName的所有節點,如果該節點有子節點,則不能刪除 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <param name="nodeName"></param> /// <returns></returns> public static bool deleteNode(string xmlFileName, string xPath, string nodeName) { bool isSuccess = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { if (xmlnode.HasChildNodes) { isSuccess = false; } else { xmlnode.ParentNode.RemoveChild(xmlnode);//刪除節點 isSuccess = true; xmldocument.Save(xmlFileName); } } } catch (Exception err) { throw err; } return isSuccess; } /// <summary> /// 根據指定節點名稱更新其下指定的子節點的值 /// </summary> /// <param name="xmlFileName"></param> /// <param name="xPath"></param> /// <param name="nodeName"></param> /// <param name="innerText"></param> /// <returns></returns> public static bool UpdateChildNode(string xmlFileName, string xPath, string nodeName,string childName, string innerText) { bool isSuccess = false; XmlDocument xmldocument = new XmlDocument(); xmldocument.Load(xmlFileName); try { XmlNode xmlnode = xmldocument.SelectSingleNode(xPath); if (xmlnode != null) { foreach (XmlNode node in xmlnode.ChildNodes) { if (node.Name.ToLower() == childName.ToLower()) { node.InnerText = innerText; xmldocument.Save(xmlFileName); isSuccess = true; } } } } catch (Exception err) { throw err; } return isSuccess; } #region 創建XML的根節點 /// <summary> /// 創建XML的根節點 /// </summary> private void CreateXMLElement() { //創建一個XML對象 _xml = new XmlDocument(); if (File.Exists(_FilePath)) { //加載XML文件 _xml.Load(this._FilePath); } //為XML的根節點賦值 _element = _xml.DocumentElement; } #endregion #region 保存XML文件 /// <summary> /// 保存XML文件 /// </summary> public void Save() { //創建XML的根節點 //CreateXMLElement(); //保存XML文件 _xml.Save(this._FilePath); } #endregion //保存XML文件 #region XML文檔創建和節點或屬性的添加、修改 /// <summary> /// 創建一個XML文檔 /// </summary> /// <param name="xmlFileName">XML文檔完全文件名(包含物理路徑)</param> /// <param name="rootNodeName">XML文檔根節點名稱(須指定一個根節點名稱)</param> /// <param name="version">XML文檔版本號(必須為:"1.0")</param> /// <param name="encoding">XML文檔編碼方式</param> /// <param name="standalone">該值必須是"yes"或"no",如果為null,Save方法不在XML聲明上寫出獨立屬性</param> /// <returns>成功返回true,失敗返回false</returns> public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone) { bool isSuccess = false; try { XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone); XmlNode root = xmlDoc.CreateElement(rootNodeName); xmlDoc.AppendChild(xmlDeclaration); xmlDoc.AppendChild(root); xmlDoc.Save(xmlFileName); isSuccess = true; } catch (Exception ex) { throw ex; //這裡可以定義你自己的異常處理 } return isSuccess; } #endregion } }