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