How to export a dataset to an XML workspace document


This document was published with and applies to ArcGIS 9.3.
A 10 version also exists. A 9.2 version also exists.
Summary This article shows how to use the IGdbXmlExport interface to export schema (and optionally data) from a geodatabase using the GdbExporter 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.  The IGdbXmlExport interface is then 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 and create a name object for it.
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(databaseName, 0);
IDataset workspaceDataset = (IDataset)workspace;
IName workspaceName = workspaceDataset.FullName;

[VB.NET]
' Open the source geodatabase and create a name object for it.
Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactory()
Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(databaseName, 0)
Dim workspaceDataset As IDataset = CType(workspace, IDataset)
Dim workspaceName As IName = workspaceDataset.FullName
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 Exception("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 Exception("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 names enumerator and add the feature dataset name.
IEnumNameEdit enumNameEdit = new NamesEnumeratorClass();
enumNameEdit.Add(featureDatasetName);
IEnumName enumName = (IEnumName)enumNameEdit;

[VB.NET]
' Create a new names enumerator and add the feature dataset name.
Dim enumNameEdit As IEnumNameEdit = New NamesEnumerator()
enumNameEdit.Add(featureDatasetName)
Dim enumName As IEnumName = CType(enumNameEdit, IEnumName)
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.
Typically when name mappings are created, they are from one geodatabase to another, and conflicts may occur. When creating name mappings from a geodatabase to a new XML workspace document, the "target" may be several geodatabases, each of which may result in different conflicts. Because conflict resolution can occur when importing an XML workspace document into a geodatabase, it can be disregarded at this stage.
 
In this example, the target geodatabase in the GenerateNameMapping example is the source geodatabase; this will create the name mappings with conflicts, but the conflicts can be ignored and the application can move on to the export process.
 
In some cases, modifying the name mappings may be required, for example if the datasets should be renamed in the workspace document. For more information on working with name mappings, 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;
geoDBDataTransfer.GenerateNameMapping(enumName, workspaceName, out
  enumNameMapping);

[VB.NET]
' Create a GeoDBDataTransfer object and create a name mapping.
Dim geoDBDataTransfer As IGeoDBDataTransfer = New GeoDBDataTransfer()
Dim enumNameMapping As IEnumNameMapping = Nothing
geoDBDataTransfer.GenerateNameMapping(enumName, workspaceName, enumNameMapping)
The following code exports the dataset's schema and data by transferring the dataset(s) in the enumNameMapping enumerator to the output XML file (outputXmlFile), with Boolean parameters dictating 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. The file extension determines the format that the data will be written in. For example, the outXmlFile could be named foo.xml, foo.ZIP, or foo.Z, with the appropriate file type resulting in each case.
 

[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 GdbExporter()
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 and create a name object for it.
  IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
  IWorkspace workspace = workspaceFactory.OpenFromFile(databaseName, 0);
  IDataset workspaceDataset = (IDataset)workspace;
  IName workspaceName = workspaceDataset.FullName;

  // 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 Exception(
      "No feature datasets exist in the specified geodatabase.");
  }

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

  // Create a GeoDBDataTransfer object and create a name mapping.
  IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
  IEnumNameMapping enumNameMapping = null;
  geoDBDataTransfer.GenerateNameMapping(enumName, workspaceName, out
    enumNameMapping);

  // 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 and create a name object for it.
    Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactory()
    Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(databaseName, 0)
    Dim workspaceDataset As IDataset = CType(workspace, IDataset)
    Dim workspaceName As IName = workspaceDataset.FullName
    
    ' 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 Exception("No feature datasets exist in the specified geodatabase.")
    End If
    
    ' Create a new names enumerator and add the feature dataset name.
    Dim enumNameEdit As IEnumNameEdit = New NamesEnumerator()
    enumNameEdit.Add(featureDatasetName)
    Dim enumName As IEnumName = CType(enumNameEdit, IEnumName)
    
    ' Create a GeoDBDataTransfer object and create a name mapping.
    Dim geoDBDataTransfer As IGeoDBDataTransfer = New GeoDBDataTransfer()
    Dim enumNameMapping As IEnumNameMapping = Nothing
    geoDBDataTransfer.GenerateNameMapping(enumName, workspaceName, enumNameMapping)
    
    ' Create an exporter and export the dataset with binary geometry, not compressed,
    ' and including metadata.
    Dim gdbXmlExport As IGdbXmlExport = New GdbExporter()
    gdbXmlExport.ExportDatasets(enumNameMapping, outputXmlFile, True, False, True)
End Sub