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


How to import a dataset from XML (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Managing data > Working with feature data > Distributed geodatabases > XML import and export > How to import a dataset from XML

How to import a dataset from XML


Summary
The topic shows how to use the IGdbXmlImport interface to import schema (and optionally data) from an Extensible Markup Language (XML) workspace document into an existing geodatabase, and resolve any conflicts that might occur with existing datasets.

In this topic


Importing data or schema from an XML workspace document

Importing an XML workspace document into an existing geodatabase is a four-step process. First, the target workspace must be opened. Second, name mappings must be generated based on the workspace document and the target workspace. Third, name mappings must be checked for conflicts, and if they exist, they have to be resolved. Finally, the export process can begin, using the workspace document, the target workspace, and the name mappings.
  1. Open the target workspace. See the following code example:
[C#]
// Open the target file 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 target file 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 GdbImporter class, which implements the IGdbXmlImport interface, can be used to generate an enumerator of name mappings, given the open workspace and the path to the XML workspace document. The name mappings are generated using the schema section of the workspace document. See the following code example:
[C#]
// Create a GdbImporter and use it to generate name mappings.
IGdbXmlImport gdbXmlImport=new GdbImporterClass();
IEnumNameMapping enumNameMapping=null;
Boolean conflictsFound=gdbXmlImport.GenerateNameMapping(workspaceDocPath,
    workspace, out enumNameMapping);
[VB.NET]
' Create a GdbImporter and use it to generate name mappings.
Dim gdbXmlImport As IGdbXmlImport=New GdbImporter()
Dim enumNameMapping As IEnumNameMapping=Nothing
Dim conflictsFound As Boolean=gdbXmlImport.GenerateNameMapping(workspaceDocPath, workspace, enumNameMapping)
  1. Some of the datasets in the workspace document might have names that conflict with existing datasets in the geodatabase. Each name mapping, along with its child name mappings (if applicable), should be checked for conflicts and if a conflict exists, it should be resolved. Attempting to import a workspace document with unresolved conflicts will cause the process to fail.

    To detect conflicts, first check the return value of GenerateNameMapping, and if it is true, iterate through the name mappings in the generated name mapping enumerator. For each name mapping, check the INameMapping.NameConflicts property, and if it returns true, set the INameMapping.TargetName property to a new value. Generate the new name using the INameMapping.GetSuggestedName method, but it can be set to any valid name that is not already taken.

    Some name mappings have child name mappings, such as feature datasets. These children must also be checked for conflicts. The following code example shows how to use the INameMapping.Children property to access these name mappings.

    In some cases, modifying the name mappings might be desired, even if no conflicts exist. For example, modifying the name mappings could be used to append a suffix or prefix onto all objects from the workspace document to "flag" them as imported (that is, MyTable could become MyTable_imp). Name mappings can also be used to change a dataset's configuration keyword. For more information on these options, see, Copying and pasting geodatabase datasets.
[C#]
// Check for conflicts.
if (conflictsFound)
{
    // Iterate through each name mapping.
    INameMapping nameMapping=null;
    enumNameMapping.Reset();
    while ((nameMapping=enumNameMapping.Next()) != null)
    {
        // Resolve the mapping's conflict (if there is one).
        if (nameMapping.NameConflicts)
        {
            nameMapping.TargetName=nameMapping.GetSuggestedName(workspaceName);
        }
        // See if the mapping's children have conflicts.
        IEnumNameMapping childEnumNameMapping=nameMapping.Children;
        if (childEnumNameMapping != null)
        {
            childEnumNameMapping.Reset();
            // Iterate through each child mapping.
            INameMapping childNameMapping=null;
            while ((childNameMapping=childEnumNameMapping.Next()) != null)
            {
                if (childNameMapping.NameConflicts)
                {
                    childNameMapping.TargetName=nameMapping.GetSuggestedName
                        (workspaceName);
                }
            }
        }
    }
}
[VB.NET]
' Check for conflicts.
If conflictsFound Then
    ' Iterate through each name mapping.
    enumNameMapping.Reset()
    Dim nameMapping As INameMapping=enumNameMapping.Next()
    While Not nameMapping Is Nothing
        ' Resolve the mapping's conflict (if there is one).
        If nameMapping.NameConflicts Then
            nameMapping.TargetName=nameMapping.GetSuggestedName(workspaceName)
        End If
        
        ' See if the mapping's children have conflicts.
        Dim childEnumNameMapping As IEnumNameMapping=nameMapping.Children
        If Not childEnumNameMapping Is Nothing Then
            childEnumNameMapping.Reset()
            
            ' Iterate through each child mapping.
            Dim childNameMapping As INameMapping=childEnumNameMapping.Next()
            While Not childNameMapping Is Nothing
                If childNameMapping.NameConflicts Then
                    childNameMapping.TargetName=childNameMapping.GetSuggestedName(workspaceName)
                End If
                childNameMapping=childEnumNameMapping.Next()
            End While
        End If
        nameMapping=enumNameMapping.Next()
    End While
End If
  1. With the name mapping generated and all conflicts resolved, the import process can begin. The final parameter of IGdbXmlImport.ImportWorkspace - schemaOnly - can be used to specify if only the schema should be imported, or both the schema and the data should be imported. In this example, a value of "false" is used, indicating that both schema and data will be imported. See the following code example:
[C#]
// Import the workspace document, including both schema and data.
gdbXmlImport.ImportWorkspace(workspaceDocPath, enumNameMapping, workspace, false);
[VB.NET]
' Import the workspace document, including both schema and data.
gdbXmlImport.ImportWorkspace(workspaceDocPath, enumNameMapping, workspace, False)

Complete code example

The following is the complete code example of importing an XML workspace document into a geodatabase:
[C#]
private void ImportXmlWorkspaceDocument(String fileGdbPath, String workspaceDocPath)
{
    // Open the target file 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;

    // Create a GdbImporter and use it to generate name mappings.
    IGdbXmlImport gdbXmlImport=new GdbImporterClass();
    IEnumNameMapping enumNameMapping=null;
    Boolean conflictsFound=gdbXmlImport.GenerateNameMapping(workspaceDocPath,
        workspace, out enumNameMapping);

    // Check for conflicts.
    if (conflictsFound)
    {
        // Iterate through each name mapping.
        INameMapping nameMapping=null;
        enumNameMapping.Reset();
        while ((nameMapping=enumNameMapping.Next()) != null)
        {
            // Resolve the mapping's conflict (if there is one).
            if (nameMapping.NameConflicts)
            {
                nameMapping.TargetName=nameMapping.GetSuggestedName(workspaceName);
            }

            // See if the mapping's children have conflicts.
            IEnumNameMapping childEnumNameMapping=nameMapping.Children;
            if (childEnumNameMapping != null)
            {
                childEnumNameMapping.Reset();

                // Iterate through each child mapping.
                INameMapping childNameMapping=null;
                while ((childNameMapping=childEnumNameMapping.Next()) != null)
                {
                    if (childNameMapping.NameConflicts)
                    {
                        childNameMapping.TargetName =
                            childNameMapping.GetSuggestedName(workspaceName);
                    }
                }
            }
        }
    }

    // Import the workspace document, including both schema and data.
    gdbXmlImport.ImportWorkspace(workspaceDocPath, enumNameMapping, workspace, false)
        ;
}
[VB.NET]
Private Sub ImportXmlWorkspaceDocument(ByVal fileGdbPath As String, ByVal workspaceDocPath As String)
    ' Open the target file 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
    
    ' Create a GdbImporter and use it to generate name mappings.
    Dim gdbXmlImport As IGdbXmlImport=New GdbImporter()
    Dim enumNameMapping As IEnumNameMapping=Nothing
    Dim conflictsFound As Boolean=gdbXmlImport.GenerateNameMapping(workspaceDocPath, workspace, enumNameMapping)
    
    ' Check for conflicts.
    If conflictsFound Then
        ' Iterate through each name mapping.
        enumNameMapping.Reset()
        Dim nameMapping As INameMapping=enumNameMapping.Next()
        While Not nameMapping Is Nothing
            ' Resolve the mapping's conflict (if there is one).
            If nameMapping.NameConflicts Then
                nameMapping.TargetName=nameMapping.GetSuggestedName(workspaceName)
            End If
            
            ' See if the mapping's children have conflicts.
            Dim childEnumNameMapping As IEnumNameMapping=nameMapping.Children
            If Not childEnumNameMapping Is Nothing Then
                childEnumNameMapping.Reset()
                
                ' Iterate through each child mapping.
                Dim childNameMapping As INameMapping=childEnumNameMapping.Next()
                While Not childNameMapping Is Nothing
                    If childNameMapping.NameConflicts Then
                        childNameMapping.TargetName=childNameMapping.GetSuggestedName(workspaceName)
                    End If
                    childNameMapping=childEnumNameMapping.Next()
                End While
            End If
            nameMapping=enumNameMapping.Next()
        End While
    End If
    
    ' Import the workspace document, including both schema and data.
    gdbXmlImport.ImportWorkspace(workspaceDocPath, enumNameMapping, workspace, False)
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 Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine: Geodatabase Update