Jaa


How to Export a Catalog to an XML File

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

To export a catalog to an XML file

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

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

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

Example

This example exports a catalog "Books" 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 ExportCatalog(CatalogContext context)
{
    // 3 second refresh interval.
    const int RefreshInterval = 3000;

    // Export the catalog "Books" in the database
    ProductCatalog catalog = context.GetCatalog("Books");

    // Create CatalogExportOptions and set the options 
    // for the export operation.
    CatalogExportOptions exportOptions = new CatalogExportOptions();
    exportOptions.CatalogsToExport = catalog.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 path1 = Path.GetTempPath();
    string path2 = "testfile.xml";
    string fileName = Path.Combine(path1, path2);

    // Monitor the progress of the import operation.
    ExportProgress progress = context.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

How to Create a CatalogContext Object

How to Export Selected Fields of a Catalog

What Are the Export Options?

Importing Catalog Data by Using the Catalog API