sql2005的xml字段类型在.net中的应用

网络整理 - 07-26
今天要介绍的就是sql2005的xml字段类型在.net中的应用。调用过程是:先运用并行化的办法把xml字段类型中的数据转换成Model对象,对Model对象操作后,再运用串行化的方法把Model对象转变成xml格式,最后存储到数据库中。
       我认为如果把复杂的业务关系数据存储在xml字段中,可简化数据库的设计,方便业务的处理。

       这里写了个小demo:
     
      假如我们有很多店铺信息,每个店铺都有一个ShopID, 所以我们就把同一店铺的信息放在以ShopID命名的文件夹下,当一台服务器放不下时,我们就部署多台,这样每台服务器存储的店铺是不一样的。这些服务器就构成了一个服务器群。出于需要,我们要把这个群复制多个,部署在不同的地区(注意,各个群的信息是相同的)。为了完成这个目的,我们先设计了数据模型 MServerGroup(服务器群信息),MServer(服务器群下的服务器信息),MServerShop(服务器对应的店铺):

    /// <summary>
    /// 服务器群信息
    /// </summary>
    /// <remarks>
    /// 用于存放点播文件服务器群的信息,比如主站的,北京站的,上海站的;各个站的数据相同.
    /// 服务器群的目的是分散数据库的压力.
    /// 目前只有主站的.
    /// </remarks>
    [Serializable()]
    public class MServerGroup : BaseModelEntity
    {
        #region private
        private int _ServerGroupID;
        private string _ServerGroupName;
        private MServerCollection _Servers;
        #endregion

        #region constructor
        /// <summary>
        /// 服务器群信息
        /// </summary>
        public MServerGroup()
        {
        }

        /// <summary>
        /// 服务器群信息
        /// </summary>
        /// <param name="_ServerGroupID">服务器群ID</param>
        /// <param name="_ServerGroupName">服务器群名称</param>
        public MServerGroup(int _ServerGroupID, string _ServerGroupName)
        {
            this._ServerGroupID = _ServerGroupID;
            this._ServerGroupName = _ServerGroupName;
        }
        #endregion

        #region property
        /// <summary>
        /// 服务器群ID
        /// </summary>
        public int ServerGroupID
        {
            get
            {
                return _ServerGroupID;
            }
            set
            {
                this._ServerGroupID = value;
            }
        }

        /// <summary>
        /// 服务器群名称
        /// </summary>
        public string ServerGroupName
        {
            get
            {
                return _ServerGroupName;
            }
            set
            {
                this._ServerGroupName = value;
            }
        }

        /// <summary>
        /// 服务器群下的服务器集合
        /// </summary>
        public MServerCollection Servers
        {
            get
            {
                return _Servers;
            }
            set
            {
                this._Servers = value;
            }
        }
        #endregion
    }


    /// <summary>
    /// 服务器群下的服务器信息
    /// </summary>
    /// <remarks>
    /// 用于存放点播文件的服务信息
    /// </remarks>
    [Serializable()]
    public class MServer : BaseModelEntity
    {
        #region private
        private int _ServerID;
        private string _ServerName;
        private string _IP;
        private string _DomainName;
        private string _Dir;
        private string _Url;
        private int _ServerGroupID;
        private MServerShopCollection _ServerShops;
        #endregion

        #region constructor
        /// <summary>
        /// 服务器信息
        /// </summary>
        public MServer()
        {
        }

        /// <summary>
        /// 服务器信息
        /// </summary>
        /// <param name="_ServerID">服务器ID</param>
        /// <param name="_ServerName">服务器名称</param>
        /// <param name="_IP">服务器IP</param>
        /// <param name="_DomainName">服务器域名</param>
        /// <param name="_Dir">文件存放目录</param>
        /// <param name="_Url">文件存放Url</param>
        /// <param name="_ServerGroupID">对应的服务器群ID</param>
        /// <param name="_ServerShops">服务器对应的店铺信息</param>
        public MServer(int _ServerID, string _ServerName, string _IP, string _DomainName, string _Dir, string _Url, int _ServerGroupID, MServerShopCollection _ServerShops)
        {
            this._ServerID = _ServerID;
            this._ServerName = _ServerName;
            this._IP = _IP;
            this._DomainName = _DomainName;
            this._Dir = _Dir;
            this._Url = _Url;
            this._ServerGroupID = _ServerGroupID;
            this._ServerShops = _ServerShops;
        }

        /// <summary>
        /// 服务器信息
        /// </summary>
        /// <param name="_ServerID">服务器ID</param>
        /// <param name="_ServerName">服务器名称</param>
        /// <param name="_IP">服务器IP</param>
        /// <param name="_DomainName">服务器域名</param>
        /// <param name="_Dir">文件存放目录</param>
        /// <param name="_Url">文件存放Url</param>
        /// <param name="_ServerGroupID">对应的服务器群ID</param>
        /// <param name="_xmlStrServerShops">服务器对应的店铺信息的xml字符串</param>
        public MServer(int _ServerID, string _ServerName, string _IP, string _DomainName, string _Dir, string _Url, int _ServerGroupID, string _xmlStrServerShops)
        {
            this._ServerID = _ServerID;
            this._ServerName = _ServerName;
            this._IP = _IP;
            this._DomainName = _DomainName;
            this._Dir = _Dir;
            this._Url = _Url;
            this._ServerGroupID = _ServerGroupID;
            this._ServerShops = Common.Utilities.SerializationHelper<MServerShopCollection>.FromXML(_xmlStrServerShops);
        }
        #endregion

        #region property
        /// <summary>
        /// 服务器ID
        /// </summary>
        public int ServerID
        {
            get
            {
                return _ServerID;
            }
            set
            {
                this._ServerID = value;
            }
        }

        /// <summary>
        /// 服务器名称
        /// </summary>
        public string ServerName
        {
            get
            {
                return _ServerName;
            }
            set
            {
                this._ServerName = value;
            }
        }

        /// <summary>
        /// 服务器IP
        /// </summary>
        public string IP
        {
            get
            {
                return _IP;
            }
            set
            {
                this._IP = value;
            }
        }

        /// <summary>
        /// 服务器域名
        /// </summary>
        public string DomainName
        {
            get
            {
                return _DomainName;
            }
            set
            {
                this._DomainName = value;
            }
        }

        /// <summary>
        /// 文件存放目录
        /// </summary>
        public string Dir
        {
            get
            {
                return Dir;
            }
            set
            {
                this.Dir = value;
            }
        }

        /// <summary>
        /// 文件存放Url
        /// </summary>
        public string Url
        {
            get
            {
                return _Url;
            }
            set
            {
                this._Url = value;
            }
        }


        /// <summary>
        /// 对应的服务器群ID
        /// </summary>
        public int ServerGroupID
        {
            get
            {
                return _ServerGroupID;
            }
            set
            {
                this._ServerGroupID = value;
            }
        }


        /// <summary>
        /// 服务器对应的店铺信息
        /// </summary>
        public MServerShopCollection ServerShops
        {
            get
            {
                return _ServerShops;
            }
            set
            {
                this._ServerShops = value;
            }
        }
        #endregion
    }
      

    /// <summary>
    /// 服务器对应的店铺
    /// </summary>
    /// <remarks>
    /// 用于存放和服务器对应的店铺
    /// </remarks>
    [Serializable()]
    [XmlRoot(ElementName = "Shop", Namespace = "")]
    public class MServerShop : BaseModelEntity
    {
        #region private
        private int _ShopID;
        private string _ShopName;
        #endregion

        #region constructor
        /// <summary>
        /// 服务器对应的店铺信息
        /// </summary>
        public MServerShop()
        {
        }

        /// <summary>
        /// 服务器对应的店铺信息
        /// </summary>
        /// <param name="_ShopID">店铺ID</param>
        /// <param name="_ShopName">店铺名称</param>
        public MServerShop(int _ShopID, string _ShopName)
        {
            this._ShopID = _ShopID;
            this._ShopName = _ShopName;
        }
        #endregion

        #region property
        /// <summary>
        /// 店铺ID
        /// </summary>
        [XmlAttribute]
        public int ShopID
        {
            get
            {
                return _ShopID;
            }
            set
            {
                this._ShopID = value;
            }
        }

        /// <summary>
        /// 店铺名称
        /// </summary>
        [XmlAttribute]
        public string ShopName
        {
            get
            {
                return _ShopName;
            }
            set
            {
                this._ShopName = value;
            }
        }
        #endregion
    }

   为了对模型的集合信息进行描述,我们有设计了MServerGroupCollection(服务器群信息集合),MServer(服务器群下的服务器信息),MServerShopCollection(服务器对应的店铺集合)

    /// <summary>
    /// 服务器群信息集合
    /// </summary>
    /// <remarks>
    [Serializable()]
    [XmlRoot("ServerGroups")]
    public class MServerGroupCollection : List<MServerGroup>
    {
        /// <summary>
        /// 服务器群信息集合
        /// </summary>
        public MServerGroupCollection()
        {
            this._MServerGroups = new List<MServerGroup>();
        }

        private List<MServerGroup> _MServerGroups;

        public List<MServerGroup> MServerGroups
        {
            get
            {
                return this._MServerGroups;
            }
            set
            {
                this._MServerGroups = value;
            }
        }
    }

    /// <summary>
    /// 服务器群下的服务器信息集合
    /// </summary>
    [XmlRoot("Servers")]
    [Serializable()]
    public class MServerCollection : List<MServer>
    {
        /// <summary>
        /// 服务器群下的服务器信息集合
        /// </summary>
        public MServerCollection()
        {
            this._MServers = new List<MServer>();
        }

        private List<MServer> _MServers;

        public List<MServer> MServers
        {
            get
            {
                return this._MServers;
            }
            set
            {
                this._MServers = value;
            }
        }
    }

    /// <summary>
    /// 服务器对应的店铺集合
    /// </summary>
    [Serializable()]
    [XmlRoot(ElementName = "Shops", Namespace = "")]
    public class MServerShopCollection
    {
        private List<MServerShop> _MServerShops;

        [XmlElement("Shop")]
        public List<MServerShop> MServerShops
        {
            get
            {
                return this._MServerShops;
            }
            set
            {
                this._MServerShops = value;
            }
        }
        /// <summary>
        /// 服务器对应的店铺集合类
        /// </summary>
        public MServerShopCollection()
        {
            this._MServerShops = new List<MServerShop>();
        }

    }