⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 groupduty.cs

📁 C#中对XML的操作
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace OA.User
{
    /// <summary>
    /// 对组织结构进行管理
    /// 数据存放:XML
    /// </summary>
    public class GroupDuty
    {
        #region 字段
        private string _name;
        private string _permission="";
        private string _nexus = "";
        private string _nodeName = "Groups";
        private XmlDocument _xmlDocument;
        private string _xmlFile;
        #endregion
        #region 属性
        /// <summary>
        /// 名字
        /// </summary>
        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }
        /// <summary>
        /// 权限
        /// </summary>
        public string Permission
        {
            get { return this._permission; }
            set { this._permission = value; }
        }
        /// <summary>
        /// 是否是职务
        /// </summary>
        public string Nexus 
        {
            get { return this._nexus; }
            set { this._nexus = value; }
        }
        /// <summary>
        /// 是部门还是职位
        /// </summary>
        public string NodeName { get { return this._nodeName; } set { this._nodeName = value; } }
        #endregion
        #region 构造
        public GroupDuty(string xmlFile) 
        {
            this._xmlDocument = new XmlDocument();
            this._xmlFile = xmlFile;
            this._xmlDocument.Load(xmlFile);
        }
        #endregion
        #region  公用方法

        /// <summary>
        /// 得到节点
        /// </summary>
        public void GetNodeByNexus() 
        {
            XmlNode data = this._xmlDocument.SelectSingleNode("//"+this._nodeName+"[@Nexus='"+this._nexus+"']");
            this._name = data.Attributes["Name"].InnerText;
            this._permission = data.Attributes["Permission"].InnerText;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="nextOrPre">指定节点排列顺序</param>
        /// <returns>是否成功</returns>
        public bool Grow(string nextOrPre,string oldNexus) 
        {
            XmlNode data = this._xmlDocument.SelectSingleNode("//" + this._nodeName + "[@Nexus='" + oldNexus + "']");
            if (data==null) return false;
            data.Attributes["Name"].InnerText = this._name;
            data.Attributes["Nexus"].InnerText = this._nexus;
            data.Attributes["Permission"].InnerText = this._permission;

            if (nextOrPre != null) 
            {
                XmlNode rootNode = data.ParentNode;
                if (nextOrPre == "p")
                {
                    if (data.PreviousSibling == null) return;
                    rootNode.InsertBefore(data.Clone(), data.PreviousSibling);
                }
                else
                {
                    if (data.NextSibling == null) return;
                    rootNode.InsertAfter(data.Clone(), data.NextSibling);
                }
                   data.ParentNode.RemoveChild(data);
            }
            this._xmlDocument.Save(this._xmlFile);
            return true;
        }
        
        /// <summary>
        /// 建立一个新节点
        /// </summary>
        /// <param name="parentNexus">父级节点的关系链</param>
        /// <param name="isAfter">是否放在什么之前</param>
        /// <param name="listAfter">放在什么之后,这个包括两个内容,一个是节点名,一个是关系链,如:“Duty|总部-技术部,软件工程师”或“Groups|总部-技术部”</param>
        /// <returns>返回成功与否</returns>
        public bool Create(string parentNexus,string isAfter,string listAfter) 
        {
            
            XmlNode rootNode;
            int nodeLay = 0;
            if (parentNexus == "0")
                rootNode = this._xmlDocument.SelectSingleNode("OA");
            else
                rootNode = this._xmlDocument.SelectSingleNode("//Groups[@Nexus='" + parentNexus + "']");
            
            if(rootNode==null)
                rootNode = this._xmlDocument.SelectSingleNode("OA");



            XmlElement objChildNode = this._xmlDocument.CreateElement(this._nodeName);
            objChildNode.SetAttribute("Name", this._name);
            objChildNode.SetAttribute("Permission", this._permission);
            objChildNode.SetAttribute("Nexus", this._nexus);
            if (isAfter == "y" || listAfter == "")
                rootNode.AppendChild(objChildNode);
            else
            {
                string[] tmp = listAfter.Split("|");
                rootNode.InsertBefore(objChildNode, this._xmlDocument.SelectSingleNode("//"+tmp[0]+"[@Nexus='" + tmp[1] + "']"));
            }
            this._xmlDocument.Save(this._xmlFile);
            return true;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="node">指定节点</param>
        public void Del() 
        {
            XmlNode data = this._xmlDocument.SelectSingleNode("//Groups[@Nexus='" + this._nexus + "']");
            if (!data) data = this._xmlDocument.SelectSingleNode("//Duty[@Nexus='" + this._nexus + "']");
            data.ParentNode.RemoveChild(data);
            this._xmlDocument.Save(this._xmlFile);
        }
        #endregion

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -