How to export a dataset to XML


Summary
This topic 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.


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.
  1. The following code example opens an existing file geodatabase:
[C#]
// Open the source geodatabase and create a name object for it.
Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
    (factoryType);
IWorkspace workspace = workspaceFactory.OpenFromFile(fileGdbPath, 0);
IDataset workspaceDataset = (IDataset)workspace;
IName workspaceName = workspaceDataset.FullName;
[VB.NET]
' Open the source geodatabase and create a name object for it.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(fileGdbPath, 0)
Dim workspaceDataset As IDataset = CType(workspace, IDataset)
Dim workspaceName As IName = workspaceDataset.FullName
  1. The following code example 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
  1. A new NamesEnumeratorClass is then created and the feature dataset name added to it. See the following code example:
[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)
  1. The following code example 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 might occur. When creating name mappings from a geodatabase to a new XML workspace document, the "target" might be several geodatabases, each of which can 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 the following code 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 can be required, for example, if the datasets should be renamed in the workspace document. For more information on working with name mappings, see Copying and pasting geodatabase datasets.
[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)
  1. The following code example exports the dataset's schema and data by transferring the dataset or datasets 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 example is the complete method that performs all of the steps shown in the previous section:
[C#]
private void ExportDatasetToXML(String fileGdbPath, String outputXmlFile)
{
    // Open the source geodatabase and create a name object for it.
    Type factoryType = Type.GetTypeFromProgID(
        "esriDataSourcesGDB.FileGDBWorkspaceFactory");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
        (factoryType);
    IWorkspace workspace = workspaceFactory.OpenFromFile(fileGdbPath, 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 fileGdbPath As String, ByVal outputXmlFile As String)
    ' Open the source geodatabase and create a name object for it.
    Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
    Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
    Dim workspace As IWorkspace = workspaceFactory.OpenFromFile(fileGdbPath, 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






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine: Geodatabase Update