C#+ArcEngine 序列化和反序列化AE對象

http://www.cnblogs.com/jindin/archive/2009/07/23/1529695.htmlhtml

在AE開發過程,老是要將某些對象暫時存儲起來,像element,layer,map,symbol 等等. ArcEngine提供了序列化對象的方法來保存這些信息, 全部能序列化的對象都支持IPersistStream 接口(詳查幫組文檔) .此外IXMLStream 也是比較重要的接口.具體請查看幫助文檔.xml

/// <summary>
        /// 序列化(將對象序列化成xml文件)
        /// </summary>
        /// <param name="xmlfile">序列化文件路徑</param>
        /// <param name="obj">序列化對象</param>
        /// <returns></returns>
        public static bool xmlSerializer(string xmlfile,object obj)
        {
            try
            {
                //判斷是否支持IPersistStream接口,只有支持該接口的對象才能進行序列化
                if (obj is ESRI.ArcGIS.esriSystem.IPersistStream)
                {
                    ESRI.ArcGIS.esriSystem.IPersistStream pStream = obj as ESRI.ArcGIS.esriSystem.IPersistStream;

                    ESRI.ArcGIS.esriSystem.IXMLStream xmlStream = new ESRI.ArcGIS.esriSystem.XMLStreamClass();

                    pStream.Save(xmlStream as ESRI.ArcGIS.esriSystem.IStream, 0);

                    xmlStream.SaveToFile(xmlfile);

                    return true;
                }
                return false;
            }
            catch (System.Exception e)
            {
                return false;
            }           
        }



--------------------------------------------------------------------------------


        /// <summary>
        /// 反序列化(將xml反序列化成指定的對象)
        /// </summary>
        /// <param name="xmlPathFile">序列化文件</param>
        /// <param name="obj">序列化對象</param>
        /// <returns></returns>
        public static bool XmlDeSerializer(string xmlPathFile,ref object obj)        
        {    
            try
            {
                //判斷文件是否存在
                if (System.IO.File.Exists(xmlPathFile) && System.IO.Path.GetExtension(xmlPathFile) == ".xml")
                {
                    ESRI.ArcGIS.esriSystem.IPersistStream pStream = obj as ESRI.ArcGIS.esriSystem.IPersistStream;

                    ESRI.ArcGIS.esriSystem.IXMLStream xmlStream = new ESRI.ArcGIS.esriSystem.XMLStreamClass();

                    xmlStream.LoadFromFile(xmlPathFile);
                    pStream.Load(xmlStream as ESRI.ArcGIS.esriSystem.IStream);

                    return true;
                }
                return false;
            }
            catch(Exception ex)
            {
                return false;
            }
        }  htm

相關文章
相關標籤/搜索