How to Import Inventory Data from an XML File

This topic describes how to import inventory data from an XML file. It describes how to set the options to control an import operation.

To import inventory data from an XML file

  1. Create an InventoryImportOptions object and set the properties for your project. This object contains options that control the behavior of the import operation.

  2. Start the import operation by calling one of the ImportXml methods of the InventoryContext object.

  3. Monitor the status of the import operation and look for errors by using the Status property of the ImportProgress object returned by the ImportXml method.

Example

This example imports all catalogs in the XML file "testfile" in the temp directory of this computer. The import is a full, non-transactional import. The file is validated as it is imported. If the import fails, the errors are written to the console.

public static void ImportInventoryCatalog(InventoryContext inventoryContext)
{
    // 3 second refresh interval.
    const int RefreshInterval = 3000;

    // Create InventoryImportOptions and set the options 
    // for the import operation.
    InventoryImportOptions importOptions = new InventoryImportOptions();

    // Import and replace all catalogs.
    importOptions.Mode = ImportMode.Full;
    importOptions.TransactionMode = TransactionMode.NonTransactional;
    importOptions.Operation = ImportOperation.ValidateAndImport;

    // The XML file is in the temp directory on this computer.
    string fileName = Path.GetTempPath() + @"testfile.xml";

    // Monitor the progress of the import operation.
    ImportProgress importProgress = inventoryContext.ImportXml(importOptions, fileName);
    while (importProgress.Status == CatalogOperationsStatus.InProgress)
    {
        System.Threading.Thread.Sleep(RefreshInterval);
        importProgress.Refresh();
    }

    // If the import operation failed, write the errors to the console.
    if (importProgress.Status == CatalogOperationsStatus.Failed)
    {
        foreach (CatalogError error in importProgress.Errors)
        {
            Console.WriteLine(string.Format("Line number: {0} Error message: {1}",
                error.LineNumber, error.Message));
        }
    }
}

See Also

Other Resources

Concepts of the Catalog System

Exporting Inventory Data

How to Create an InventoryContext Object