Поделиться через


How to Import a Catalog from an XML File

This topic describes how to import a catalog from an XML file. It describes how to set the Commerce Server Core Systems options to control an import operation.

How to import from an XML file

  1. Create a CatalogImportOptions 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 CatalogContext 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 a catalog "Books" from an 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 ImportCatalog(CatalogContext context)
{
    // 3 second refresh interval.
    const int RefreshInterval = 3000;

    // Create CatalogImportOptions and set the options 
    // for the import operation.
    CatalogImportOptions importOptions = new CatalogImportOptions();
    importOptions.Mode = ImportMode.Full;
    importOptions.TransactionMode = TransactionMode.NonTransactional;
    importOptions.CatalogsToImport = "Books";
    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 = context.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

How to Create a CatalogContext Object

How to Import Selected Fields of a Catalog

What Are the Import Options?

How to Create a Base Catalog by Using the Catalog API