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


Deserialize an XML File Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Deserialize an XML File Snippet

Deserialize an XML file and return its contents using the XMLSerializer.

[C#]
///<summary>Deserialize an XML file and return its contents using the XMLSerializer</summary>
/// 
///<param name="xmlPathFile">A System.String that is the fully qualified path and file name to an XML file. Example: "C:\temp\mydata.xml"</param>
///  
///<returns>A System.Object that is XML</returns>
///  
///<remarks></remarks>
public System.Object DeserializeAnXMLFile(System.String xmlPathFile)
{
  // Create xmlStream and load in the .XML file
  ESRI.ArcGIS.esriSystem.IXMLStream xmlStream=new ESRI.ArcGIS.esriSystem.XMLStreamClass();
  xmlStream.LoadFromFile(xmlPathFile);

  // Create xmlReader and read the XML stream
  ESRI.ArcGIS.esriSystem.IXMLReader xmlReader=new ESRI.ArcGIS.esriSystem.XMLReaderClass();
  xmlReader.ReadFrom((ESRI.ArcGIS.esriSystem.IStream)xmlStream); // Explicit Cast

  // Create a serializer
  ESRI.ArcGIS.esriSystem.IXMLSerializer xmlSerializer=new ESRI.ArcGIS.esriSystem.XMLSerializerClass();

  // Return the XML contents
  return xmlSerializer.ReadObject(xmlReader, null, null);
}
[Visual Basic .NET]
'''<summary>Deserialize an XML file and return its contents using the XMLSerializer</summary>
''' 
'''<param name="xmlPathFile">A System.String that is the fully qualified path and file name to an XML file. Example: "C:\temp\mydata.xml"</param>
'''  
'''<returns>A System.Object that is XML</returns>
'''  
'''<remarks></remarks>
Public Function DeserializeAnXMLFile(ByVal xmlPathFile As System.String) As System.Object

  ' Create xmlStream and load in the .XML file
  Dim xmlStream As ESRI.ArcGIS.esriSystem.IXMLStream=New ESRI.ArcGIS.esriSystem.XMLStreamClass
  xmlStream.LoadFromFile(xmlPathFile)

  ' Create xmlReader and read the XML stream
  Dim xmlReader As ESRI.ArcGIS.esriSystem.IXMLReader=New ESRI.ArcGIS.esriSystem.XMLReaderClass
  xmlReader.ReadFrom(CType(xmlStream, ESRI.ArcGIS.esriSystem.IStream))

  ' Create a serializer
  Dim xmlSerializer As ESRI.ArcGIS.esriSystem.IXMLSerializer=New ESRI.ArcGIS.esriSystem.XMLSerializerClass

  ' Return the XML contents
  Return xmlSerializer.ReadObject(xmlReader, Nothing, Nothing)

End Function

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