During the AE development process, some objects must be temporarily stored, such as element, layer, map, and symbol. arcengine provides methods for serializing objects to save this information. All serializable objects support the ipersiststream interface (For details, refer to the help group documentation ). ixmlstream is also an important interface. for more information, see the help documentation.
This article is inspired by the technical documents of wall daniu.
Address: http://www.cnblogs.com/wall/archive/2009/02/26/1398447.html
Code
/// <Summary>
/// Serialize (serialize an object into an XML file)
/// </Summary>
/// <Param name = "xmlfile"> serialize the file path </param>
/// <Param name = "OBJ"> serialize an object </param>
/// <Returns> </returns>
Public static bool xmlserializer (string xmlfile, object OBJ)
{
Try
{
// Determine whether the ipersiststream interface is supported. Only objects that support this interface can be serialized.
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>
/// Deserialization (deserializing XML into a specified object)
/// </Summary>
/// <Param name = "xmlpathfile"> serialize a file </param>
/// <Param name = "OBJ"> serialize an object </param>
/// <Returns> </returns>
Public static bool xmldeserializer (string xmlpathfile, ref object OBJ)
{
Try
{
// Determine whether a file exists
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;
}
}