How to export a dataset to an XML workspace document


Summary This article shows how to use the IGdbXmlExport interface to export schema and data or schema from a geodatabase using the GdbExporter co-class. This example shows how to create a method that accepts the database name as a parameter to create an Extensible Markup Language (XML) file for an existing geodatabase.

Development licensing Deployment licensing
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime: Geodatabase Update

To use the code in this article, the following namespaces must be referenced via the using (C#) or Imports (VB.NET) statements. It is also necessary to add the corresponding references to the project in order to gain access to these APIs.
 
The following assembly does not require a using or imports statement, but must be referenced by the project.

In this topic

 

Exporting a dataset to an XML workspace document

The steps in this section show how to export a feature dataset from a personal geodatabase.  After opening a workspace, a name object is retrieved for the first feature dataset found.  A scratch workspace is then created and cast to the IName interface for the sake of creating a name mapping (necessary for the export process).  Finally, the IGdbXmlExport interface is used to write the contents of the feature dataset to a new XML document.
 
The following code opens an existing personal geodatabase:
 

[C#]
// Open the source geodatabase.
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(databaseName, 0);

[VB.NET]
' Open the source geodatabase.
Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactoryClass()
Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(databaseName, 0)
The following code retrieves a name object for the first feature dataset in the geodatabase.  If no feature datasets are found, an exception will be thrown.
 

[C#]
// Retrieve the first feature dataset from the workspace.
IEnumDatasetName enumDatasetName = workspace.get_DatasetNames
  (esriDatasetType.esriDTFeatureDataset);
enumDatasetName.Reset();
IName featureDatasetName = (IName)enumDatasetName.Next();
if (featureDatasetName == null)
{
  throw new ArgumentException(
    "No feature datasets exist in the specified geodatabase.");
}

[VB.NET]
' Retrieve the first feature dataset from the workspace.
Dim enumDatasetName As IEnumDatasetName = workspace.DatasetNames(esriDatasetType.esriDTFeatureDataset)
enumDatasetName.Reset()
Dim featureDatasetName As IName = CType(enumDatasetName.Next(), IName)
If featureDatasetName Is Nothing Then
    Throw New ArgumentException("No feature datasets exist in the specified geodatabase.")
End If
A new NamesEnumeratorClass is then created, and the feature dataset name added to it:
[C#]
// Create a new enumerator and add the feature dataset name.
IEnumNameEdit enumNameEdit = new NamesEnumeratorClass();
enumNameEdit.Add(featureDatasetName);
IEnumName enumName = (IEnumName)enumNameEdit;

[VB.NET]
' Create a new enumerator and add the feature dataset name.
Dim enumNameEdit As IEnumNameEdit = New NamesEnumeratorClass()
enumNameEdit.Add(featureDatasetName)
Dim enumName As IEnumName = CType(enumNameEdit, IEnumName)
The following code creates a new scratch workspace and casts it to the IName interface to prepare for name mapping:
 
 

[C#]
// Create a scratch workspace factory. 
IScratchWorkspaceFactory scratchWorkspaceFactory = new
  ScratchWorkspaceFactoryClass();
IWorkspace scratchWorkspace = scratchWorkspaceFactory.CreateNewScratchWorkspace
  ();
IDataset dataset = (IDataset)scratchWorkspace;
IName workspaceName = dataset.FullName;

[VB.NET]
' Create a scratch workspace factory.
Dim scratchWorkspaceFactory As IScratchWorkspaceFactory = New ScratchWorkspaceFactoryClass()
Dim scratchWorkspace As IWorkspace = scratchWorkspaceFactory.CreateNewScratchWorkspace()
Dim dataset As IDataset = CType(scratchWorkspace, IDataset)
Dim workspaceName As IName = dataset.FullName
The following code will create a GeoDBDataTransfer object and use it to generate a name mapping, which checks for conflicts that will occur during the data transfer.
 
 
 
In the scenario presented by this example, conflicts should not occur, since the mapping is generated between an existing Access geodatabase and a newly-created scratch workspace, which is also base on an Access database.  If the XML document is being created for transfer between different data source types, or to a geodatabase with existing data, conflicts may occur.  For more information on handling conflicts, see the article How to use copy and paste to transfer data.

[C#]
// Create a GeoDBDataTransfer object and create a name mapping.
IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
IEnumNameMapping enumNameMapping = null;
Boolean hasConflicts = geoDBDataTransfer.GenerateNameMapping(enumName,
  workspaceName, out enumNameMapping);
if (hasConflicts)
{
  // TODO: Handle this in a way appropriate to your application.
}

[VB.NET]
' Create a GeoDBDataTransfer object and create a name mapping.
Dim geoDBDataTransfer As IGeoDBDataTransfer = New GeoDBDataTransferClass()
Dim enumNameMapping As IEnumNameMapping = Nothing
Dim hasConflicts As Boolean = geoDBDataTransfer.GenerateNameMapping(enumName, workspaceName, enumNameMapping)
If hasConflicts Then
    ' TODO: Handle this in a way appropriate to your application.
End If
The following code exports the dataset schema and data by transferring the dataset(s) in the enumNameMapping enumerator to the output XML file (outputXmlFile), with boolean parameters set so that the geometry is exported in binary format, the XML document is not compressed, and the dataset metadata is included.
 
The outputXmlFile is a string that identifies the output XML file, and includes the file extension. For example, the outXmlFile could have the following extension of foo.xml, foo.ZIP, or foo.Z:
 
 

[C#]
// Create an exporter and export the dataset with binary geometry, not compressed,
// and including metadata.
IGdbXmlExport gdbXmlExport = new GdbExporterClass();
gdbXmlExport.ExportDatasets(enumNameMapping, outputXmlFile, true, false, true);

[VB.NET]
' Create an exporter and export the dataset with binary geometry, not compressed,
' and including metadata.
Dim gdbXmlExport As IGdbXmlExport = New GdbExporterClass()
gdbXmlExport.ExportDatasets(enumNameMapping, outputXmlFile, True, False, True)

Complete code example

The following code is a complete method which performs all of the steps shown in the previous section:
 
 

[C#]
private void ExportDatasetToXML(String databaseName, String outputXmlFile)
{
  // Open the source geodatabase.
  IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
  IWorkspace workspace = workspaceFactory.OpenFromFile(databaseName, 0);

  // Retrieve the first feature dataset from the workspace.
  IEnumDatasetName enumDatasetName = workspace.get_DatasetNames
    (esriDatasetType.esriDTFeatureDataset);
  enumDatasetName.Reset();
  IName featureDatasetName = (IName)enumDatasetName.Next();
  if (featureDatasetName == null)
  {
    throw new ArgumentException(
      "No feature datasets exist in the specified geodatabase.");
  }

  // Create a new enumerator and add the feature dataset name.
  IEnumNameEdit enumNameEdit = new NamesEnumeratorClass();
  enumNameEdit.Add(featureDatasetName);
  IEnumName enumName = (IEnumName)enumNameEdit;

  // Create a scratch workspace factory. 
  IScratchWorkspaceFactory scratchWorkspaceFactory = new
    ScratchWorkspaceFactoryClass();
  IWorkspace scratchWorkspace =
    scratchWorkspaceFactory.CreateNewScratchWorkspace();
  IDataset dataset = (IDataset)scratchWorkspace;
  IName workspaceName = dataset.FullName;

  // Create a GeoDBDataTransfer object and create a name mapping.
  IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
  IEnumNameMapping enumNameMapping = null;
  Boolean hasConflicts = geoDBDataTransfer.GenerateNameMapping(enumName,
    workspaceName, out enumNameMapping);
  if (hasConflicts)
  {
    // TODO: Handle this in a way appropriate to your application.
  }

  // Create an exporter and export the dataset with binary geometry, not compressed,
  // and including metadata.
  IGdbXmlExport gdbXmlExport = new GdbExporterClass();
  gdbXmlExport.ExportDatasets(enumNameMapping, outputXmlFile, true, false, true)
    ;
}

[VB.NET]
Private Sub ExportDatasetToXML(ByVal databaseName As String, ByVal outputXmlFile As String)
    
    ' Open the source geodatabase.
    Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactoryClass()
    Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(databaseName, 0)
    
    ' Retrieve the first feature dataset from the workspace.
    Dim enumDatasetName As IEnumDatasetName = workspace.DatasetNames(esriDatasetType.esriDTFeatureDataset)
    enumDatasetName.Reset()
    Dim featureDatasetName As IName = CType(enumDatasetName.Next(), IName)
    If featureDatasetName Is Nothing Then
        Throw New ArgumentException("No feature datasets exist in the specified geodatabase.")
    End If
    
    ' Create a new enumerator and add the feature dataset name.
    Dim enumNameEdit As IEnumNameEdit = New NamesEnumeratorClass()
    enumNameEdit.Add(featureDatasetName)
    Dim enumName As IEnumName = CType(enumNameEdit, IEnumName)
    
    ' Create a scratch workspace factory.
    Dim scratchWorkspaceFactory As IScratchWorkspaceFactory = New ScratchWorkspaceFactoryClass()
    Dim scratchWorkspace As IWorkspace = scratchWorkspaceFactory.CreateNewScratchWorkspace()
    Dim dataset As IDataset = CType(scratchWorkspace, IDataset)
    Dim workspaceName As IName = dataset.FullName
    
    ' Create a GeoDBDataTransfer object and create a name mapping.
    Dim geoDBDataTransfer As IGeoDBDataTransfer = New GeoDBDataTransferClass()
    Dim enumNameMapping As IEnumNameMapping = Nothing
    Dim hasConflicts As Boolean = geoDBDataTransfer.GenerateNameMapping(enumName, workspaceName, enumNameMapping)
    If hasConflicts Then
        ' TODO: Handle this in a way appropriate to your application.
    End If
    
    ' Create an exporter and export the dataset with binary geometry, not compressed,
    ' and including metadata.
    Dim gdbXmlExport As IGdbXmlExport = New GdbExporterClass()
    gdbXmlExport.ExportDatasets(enumNameMapping, outputXmlFile, True, False, True)
    
End Sub