当然可以,给你个例子

发表于:2007-06-30来源:作者:点击数: 标签:
注意类前面的[xmlinclude]那部分 using System; using System.Collections ; using System.IO ; using System.Xml.Serialization; using System.Diagnostics ; namespace Bigeagle.Model.Base { /// summary /// 枚举,表示组织结构节点类型 /// /summary pub
注意类前面的[xmlinclude]那部分


using System;
using System.Collections ;
using System.IO ;
using System.Xml.Serialization;
using System.Diagnostics ;

namespace Bigeagle.Model.Base
{

    /// <summary>
    /// 枚举,表示组织结构节点类型
    /// </summary>
    public enum ObjectType
    {
        Enterprise = 0 ,
        Outter ,
        Unit ,
        Department ,
        Position ,
        Duty ,
        Data

    }
    /// <summary>
    /// <br>组织结构类</br>
    /// <br>Author:  Bigeagle@163.net</br>
    /// <br>Date:    2001/12/26</br>
    /// <br>History: 2001/12/26 finished</br>
    /// <ul>2001/12/28,添加流程数组</ul>
    /// </summary>
    ///<remarks>组织结构类</remarks>
    [XmlInclude(typeof(Enterprise)), XmlInclude(typeof(Outter))
    , XmlInclude(typeof(Data)) , XmlInclude(typeof(Department))
    , XmlInclude(typeof(Duty)) , XmlInclude(typeof(Position))
    , XmlInclude(typeof(Unit)) , XmlInclude(typeof(ModelEvent))
    , XmlInclude(typeof(Flow))]
    public class Organize
    {

        /// <summary>
        /// 名称
        /// </summary>
        private string m_strName ;

        /// <summary>
        /// 子节点数组
        /// </summary>
        private ArrayList m_arrChilds ;

        /// <summary>
        /// 流程数组
        /// </summary>
        private ArrayList m_arrFlows ;

        /// <summary>
        /// 存取名称的属性
        /// </summary>
        public string Name
        {
            get
            {
                return this.m_strName ;
            }
            set
            {
                this.m_strName = value ;
            }
        }

        /// <summary>
        /// 存取子节点的属性
        /// </summary>
        public ArrayList Childs
        {
            get
            {
                return this.m_arrChilds ;
            }
            set
            {
                this.m_arrChilds = value ;
            }
        }

        /// <summary>
        /// 存取流程数组的属性
        /// </summary>
        public ArrayList Flows
        {
            get
            {
                return this.m_arrFlows ;
            }
            set
            {
                this.m_arrFlows = value ;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public Organize()
        {
            //
            // TODO: Add constructor logic here
            //
            this.m_strName = "" ;
            this.m_arrChilds = new ArrayList() ;
        }//end method

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="a_strFatherID">父节点的id</param>
        /// <param name="a_objChild">要添加的节点</param>
        /// <returns>新插入节点的id</returns>
        public string AddChild(string a_strFatherID , object a_objChild)
        {
            OrganizeChild objFather = (OrganizeChild)this.GetChild(a_strFatherID) ;
            OrganizeChild objChild = (OrganizeChild)a_objChild ;

//            if(objFather == null)
//            {
//                if(objChild.Type != ObjectType.Enterprise && objChild.Type != ObjectType.Outter)
//                {
//                    throw(new Exception("组织结构下只能插入企业和外部活动者!")) ;
//                }
//            }
//            else if(objFather.Type == ObjectType.Outter && objChild.Type != ObjectType.Data)
//            {
//                throw(new Exception("外部活动者下只能插入数据对象!")) ;
//            }
//            else if(objFather.Type == ObjectType.Enterprise
//                && objChild.Type != ObjectType.Unit || objChild.Type != ObjectType.Data)
//            {
//                throw(new Exception("企业下只能插入单位对象!")) ;
//            }
//
//            else if(objChild.Type != ObjectType.Data && (int)objChild.Type - (int)objFather.Type > 1)
//            {
//                throw(new Exception("要加入的节点类型错误!")) ;
//            }

            //计算id
            string strID = "" ;
            if(a_strFatherID == "0")
            {
                strID = (this.GetChildCount(a_strFatherID) + 1).ToString() ;
            }
            else
            {
                strID = a_strFatherID + "." + (this.GetChildCount(a_strFatherID) + 1).ToString() ;            
            }

            ((OrganizeChild)a_objChild).ID = strID ;
            this.m_arrChilds.Add(a_objChild) ;

            return strID ;
        }//end method


        /// <summary>
        /// 根据id返回一个child
        /// </summary>
        /// <param name="a_strID">节点id</param>
        /// <returns>如果未找到则返回null</returns>
        public object GetChild(string a_strID)
        {
//            for(int i = 0 ; i < this.m_arrChilds.Count ; i ++)
//            {
//                OrganizeChild oc = (OrganizeChild)this.m_arrChilds[i] ;
//                if(oc.ID == a_strID)
//                {
//                    return this.m_arrChilds[i] ;
//                }
//            }

            foreach(OrganizeChild o in this.m_arrChilds)
            {
                if(o.ID.Equals(a_strID))
                {
                    return o ;
                }
            }
            return null ;
        }//end method

        /// <summary>
        /// 取得指定节点的字节点数
        /// </summary>
        /// <param name="a_strID">节点id</param>
        /// <returns></returns>
        public int GetChildCount(string a_strID)
        {
            int intCount = 0 ;
            for(int i = 0 ; i < this.m_arrChilds.Count ; i ++)
            {
                OrganizeChild oc = (OrganizeChild)this.m_arrChilds[i] ;

                if(oc.ID.Length > a_strID.Length && oc.ID.Substring(0 , a_strID.Length) == a_strID)
                {
                    intCount ++ ;
                }
            }//end for

            return intCount ;
        }//end method


        /// <summary>
        /// 根据id取得父节点
        /// </summary>
        /// <param name="a_strID">节点id</param>
        /// <returns>如果没有父节点则返回null</returns>
        public object GetParent(string a_strID)
        {
            string[] str = a_strID.Split(@#.@#) ;
            if(str.Length == 1)
            {
                return null ;
            }

            foreach(OrganizeChild o in this.m_arrChilds)
            {
                string id = a_strID.Substring(0 , a_strID.Length - str[str.Length - 1].Length -1) ;
                if(o.ID == id)
                {
                    return o ;
                }
            }

            return null ;
        }//end method


        /// <summary>
        /// 根据id取得子节点数组
        /// </summary>
        /// <param name="a_strID">节点id</param>
        /// <param name="a_bAll">是否包含全部子节点</param>
        /// <returns>子节点数组</returns>
        /// <remarks>如果a_bAll为true返回所有子节点,否则只返回下一层的子节点</remarks>
        public ArrayList GetChilds(string a_strID , bool a_bAll)
        {
            ArrayList result = new ArrayList() ;
            foreach(OrganizeChild oc in this.Childs)
            {
                if(a_bAll)
                {
                    if(oc.ID.Length > a_strID.Length
                        && oc.ID.Substring(0 , a_strID.Length) == a_strID)
                    {
                        result.Add(oc) ;
                    }
                }
                else
                {
                    if(oc.ID.Length > a_strID.Length
                        && oc.ID.Substring(0 , a_strID.Length) == a_strID
                        && oc.ID.Split(@#.@#).Length - a_strID.Split(@#.@#).Length == 1)
                    {
                        result.Add(oc) ;
                    }
                }

            }

            return result ;
        }//end method

        /// <summary>
        /// 添加一个新事件
        /// </summary>
        /// <param name="a_objEvent">要添加的事件id</param>
        public void AddEvent(ModelEvent a_objEvent)
        {
            //取得事件所属的节点
            OrganizeChild parent = (OrganizeChild)this.GetParent(a_objEvent.BeginModelID) ;

#if DEBUG
            Debug.Assert(parent != null , "事件所属节点不能为空!") ;
            Debug.Assert(parent == this.GetParent(a_objEvent.EndModelID)
                , "事件开始对象和结束对象的父节点应该相同!") ;
#endif//DEBUG

            //生成id
            string strId = parent.ID + "_" ;
            if(parent.ModelEvents.Count == 0)
            {
                strId = strId + "1" ;
            }
            else
            {
                strId = strId + (int.Parse(((ModelEvent)
                    parent.ModelEvents[parent.ModelEvents.Count - 1]).ID.Split(@#_@#)[1]) + 1).ToString() ;
            }

            a_objEvent.ID = strId ;

            parent.ModelEvents.Add(a_objEvent) ;
        }//end method

        public void AddFlow(Flow a_objFlow)
        {
            //生成id
            int intID = 0 ;
            if(this.m_arrFlows.Count == 0)
            {
                intID = 1 ;
            }
            else
            {
                intID = ((Flow)this.m_arrFlows[this.m_arrFlows.Count - 1]).ID + 1 ;
            }

            a_objFlow.ID = intID ;
            this.m_arrFlows.Add(a_objFlow) ;
        }//end method

    }//end class
}//end class

原文转自:http://www.ltesting.net