How to Export an Inventory Catalog to an XML File

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

This topic describes how to export an inventory catalog to an XML file. It describes how set the options to control an export operation.

To export an inventory catalog to an XML file

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

  2. Start the export operation by calling one of the ExportXml methods of the InventoryContext object.

  3. Monitor the status of the export operation and look for errors by using the Status property of the ExportProgress object returned by the ExportXml method.

Example

This example exports the inventory catalog named "SummerClothing" to an XML file "testfile" in the temp directory of this computer. The entire catalog is exported. If the export fails, the errors are written to the console.

public static void ExportInventoryCatalog(InventoryContext inventoryContext)
{
    // Three second refresh interval.
    const int RefreshInterval = 3000; 
     
    // Export the inventory catalog "SummerClothing".
    InventoryCatalog inventoryCatalog = inventoryContext.GetInventoryCatalog("SummerClothing");

    // Create CatalogExportOptions and set the options 
    // for the export operation.
    InventoryExportOptions exportOptions = new InventoryExportOptions();
    exportOptions.CatalogsToExport = inventoryCatalog.Name;
   
    // Export the entire catalog.
    exportOptions.Mode = ExportMode.Full;
    // Do not export the schema to the XML file.
    exportOptions.SchemaExportType = SchemaExportType.None;

    // The catalog will be written to an XML file "testfile" in the 
    // temp directory on this computer.
    string fileName = Path.GetTempPath() + @"\testfile.xml";

    // Monitor the progress of the import operation.
    ExportProgress progress = inventoryContext.ExportXml(exportOptions, fileName);
    while (progress.Status == CatalogOperationsStatus.InProgress)
    {
        System.Threading.Thread.Sleep(RefreshInterval);
        progress.Refresh();
    }
    // If the export operation failed write the errors to the console.
    if (progress.Status == CatalogOperationsStatus.Failed)
    {
        foreach (CatalogError error in progress.Errors)
        {
            Console.WriteLine(string.Format("Line number: {0} Error message: {1}",
                error.LineNumber, error.Message));
        }
    }
}

See Also

Other Resources

What Are the Inventory Concepts?

How to Create an InventoryContext Object

How to Import Inventory Data from an XML File