Serializing classes to XML


Summary This topic provides a brief introduction and code example to serialize classes to Extensible Markup Language (XML). Additional information can be found in the Threading library overview topic.

Serializing ArcGIS Explorer objects

Serialization of ArcGIS Explorer objects XML is used in a number of situations; that is, when saving a map, and when passing objects between the user interface (UI) thread and a background worker thread. For a general introduction to serializing objects, see the Microsoft Developer Network Web site topic, Introducing XML Serialization.
Many ArcGIS Explorer objects can be serialized to a worker thread and back again, creating new objects to work with on a different thread that have the same state as the object on the original thread.
The ToXmlString and CreateFromXmlString methods, found on the Geometry, Graphic, MapItem, Symbol, and Viewpoint classes help you perform the serialization and deserialization. These helper methods are provided to make the code for serializing and deserializing ArcGIS Explorer objects simpler than comparable code using the .NET framework IXmlSerializable interface. Although the IXmlSerializable interface is available if you want to use it, the recommended approach is to use the ToXmlString and CreateFromXMLString. See the following code example:
[C#]
// Create a new Point and serialize it to an XML representation.
ESRI.ArcGISExplorer.Geometry.Point ptObject1 = new
    ESRI.ArcGISExplorer.Geometry.Point();
string ptXml = ptObject1.ToXmlString();
// Create a second Point by deserializing the XML string to a new object.
ESRI.ArcGISExplorer.Geometry.Point ptObject2 =
    ESRI.ArcGISExplorer.Geometry.Point.CreateFromXmlString(ptXml);
[VB.NET]
' Create a new Point and serialize it to an XML representation.
Dim ptObject1 As ESRI.ArcGISExplorer.Geometry.Point = New ESRI.ArcGISExplorer.Geometry.Point()
Dim ptXml As String = ptObject1.ToXmlString()
' Create a second Point by deserializing the XML string to a new object.
Dim ptObject2 As ESRI.ArcGISExplorer.Geometry.Point = ESRI.ArcGISExplorer.Geometry.Point.CreateFromXmlString(ptXml)
For more information on background workers and serializing classes to XML, see the Threading library overview.