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

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

在AE开发过程,总是要将某些对象暂时存储起来,像element,layer,map,symbol 等等. ArcEngine提供了序列化对象的方法来保存这些信息, 所有能序列化的对象都支持IPersistStream 接口(详查帮组文档) .此外IXMLStream 也是比较重要的接口.具体请查看帮助文档.

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

你可能感兴趣的:(ArcEngine)