This document is archived and information here might be outdated.  Recommended version.


Serialize an XML File Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Serialize an XML File Snippet

Serialize any object and save it to an xml file using the XMLSerializer.

[C#]
///<summary>Serialize any object and save it to an xml file using the XMLSerializer.</summary>
/// 
///<param name="xmlPathFile">A System.String that is the path and file name of the XML file to create. Example: "C:\temp\myfile.xml"</param>
///<param name="xmlObject">A System.Object. Any ArcObject (Example: IPoint, IPolygon, IFieldEdit, etc..) or intrinsic value (Example: string, boolean, int, etc..) can be used.</param>
///<param name="xmlNodeName">A System.String for the name of the XML node (element) that will contain the data. Example: "Point" or "Polygon" or "whatever".</param>
///  
///<remarks></remarks>
public void SerializeAnXMLFile(System.String xmlPathFile, System.Object xmlObject, System.String xmlNodeName)
{
  System.String elementURI="http://www.esri.com/schemas/ArcGIS/9.2";

  // Create xml writer
  ESRI.ArcGIS.esriSystem.IXMLWriter xmlWriter=new ESRI.ArcGIS.esriSystem.XMLWriterClass();

  // Create xml stream
  ESRI.ArcGIS.esriSystem.IXMLStream xmlStream=new ESRI.ArcGIS.esriSystem.XMLStreamClass();

  // Explicit Cast for IStream and then write to stream 
  xmlWriter.WriteTo((ESRI.ArcGIS.esriSystem.IStream)xmlStream);

  // Serialize 
  ESRI.ArcGIS.esriSystem.IXMLSerializer xmlSerializer=new ESRI.ArcGIS.esriSystem.XMLSerializerClass();
  xmlSerializer.WriteObject(xmlWriter, null, null, xmlNodeName, elementURI, xmlObject);

  // Save the xmlObject to an xml file. When using xmlstream the cpu keeps data in memory until it is written to file. 
  xmlStream.SaveToFile(@xmlPathFile);
}
[Visual Basic .NET]
'''<summary>Serialize any object and save it to an xml file using the XMLSerializer.</summary>
''' 
'''<param name="xmlPathFile">A System.String that is the path and file name of the XML file to create. Example: "C:\temp\myfile.xml"</param>
'''<param name="xmlObject">A System.Object. Any ArcObject (Example: IPoint, IPolygon, IFieldEdit, etc..) or intrinsic value (Example: string, boolean, int, etc..) can be used.</param>
'''<param name="xmlNodeName">A System.String for the name of the XML node (element) that will contain the data. Example: "Point" or "Polygon" or "whatever".</param>
'''  
'''<remarks></remarks>
Public Sub SerializeAnXMLFile(ByVal xmlPathFile As System.String, ByVal xmlObject As System.Object, ByVal xmlNodeName As System.String)

  ' xml namespace
  Dim elementURI As System.String="http://www.esri.com/schemas/ArcGIS/9.2"

  ' Create xml writer
  Dim xmlWriter As ESRI.ArcGIS.esriSystem.IXMLWriter=New ESRI.ArcGIS.esriSystem.XMLWriterClass

  ' Create xml stream
  Dim xmlStream As ESRI.ArcGIS.esriSystem.IXMLStream=New ESRI.ArcGIS.esriSystem.XMLStreamClass

  ' Write to stream
  xmlWriter.WriteTo(CType(XMLStream, ESRI.ArcGIS.esriSystem.IStream))

  ' Serialize the field
  Dim xmlSerializer As ESRI.ArcGIS.esriSystem.IXMLSerializer=New ESRI.ArcGIS.esriSystem.XMLSerializerClass
  xmlSerializer.WriteObject(xmlWriter, Nothing, Nothing, xmlNodeName, elementURI, xmlObject)

  ' When using xmlstream the cpu keeps data in memory until it is written to file. 
  XMLStream.SaveToFile(xmlPathFile)

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.System