C#+ArcEngine 序列化和還原序列化AE對象

來源:互聯網
上載者:User

在AE開發過程,總是要將某些對象暫時儲存起來,像element,layer,map,symbol 等等. ArcEngine提供了序列化對象的方法來儲存這些資訊, 所有能序列化的對象都支援IPersistStream 介面(詳查幫組文檔) .此外IXMLStream 也是比較重要的介面.具體請查看協助文檔.

 

本文參考了wall 大牛的技術文檔,並受到啟發.

相關地址: http://www.cnblogs.com/wall/archive/2009/02/26/1398447.html

 

Code
        /// <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;
            }
        }  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.