Export (0) Print
Expand All
Expand Minimize

Workbook.XmlImportXml Method (2007 System)

Imports an XML data stream that has been previously loaded into memory.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)

public XlXmlImportResult XmlImportXml(
	string Data,
	out XmlMap ImportMap,
	Object Overwrite,
	Object Destination


Type: System.String

The data to import.

Type: XmlMap%

The schema map to apply when importing the file.

Type: System.Object

If a value is not specified for the Destination parameter, then this parameter specifies whether or not to overwrite data that has been mapped to the schema map specified in the ImportMap parameter. Set to true to overwrite the data or false to append the new data to the existing data. The default value is true. If a value is specified for the Destination parameter, then this parameter specifies whether or not to overwrite existing data. Set to true to overwrite existing data or false to cancel the import if data would be overwritten. The default value is true.

Type: System.Object

The data will be imported into a new XML list in the Range specified.

Return Value

Type: XlXmlImportResult
One of the XlXmlImportResult values.

Do not specify a value for the Destination parameter if you want to import data into an existing mapping.

The following conditions will cause this method to generate run-time errors:

  • The specified XML data contains syntax errors.

  • The import process was cancelled because the specified data cannot fit into the worksheet.

Use the XmlImport method to import an XML data file into the current workbook.

Optional Parameters

For information on optional parameters, see The Variable missing and Optional Parameters in Office Solutions.

The following code example demonstrates how to import XML data into a workbook. The example creates a DataSet of customer names and adds an XmlMap based on the XML schema for the DataSet to the XmlMaps collection of the current workbook. The example then calls the XmlImportXml method to import the data into worksheet Sheet1. When the XmlImportXml method is called, the BeforeXmlImport event handler prompts the user to either proceed with or cancel importing the XML, and the AfterXmlImport event handler reports whether the XML was successfully imported.

This example is for a document-level customization.

private void WorkbookXmlImportEvents()
    this.BeforeXmlImport +=
        new Excel.WorkbookEvents_BeforeXmlImportEventHandler(

    this.AfterXmlImport += new

    // Create a new DataTable.
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("Customers");
    dt.Columns.Add(new DataColumn("LastName"));
    dt.Columns.Add(new DataColumn("FirstName"));

    // Add a new row to the DataTable.
    DataRow dr = dt.NewRow();
    dr["LastName"] = "Chan";
    dr["FirstName"] = "Gareth";

    // Add a new XML map to the collection.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(ds.GetXmlSchema(),

    // Import the data stream if the XmlMap was successfully created. 
    if (xmlMap1 != null)
        // This will raise the BeforeXmlImport and AfterXmlImport events.
        Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
        this.XmlImportXml(ds.GetXml(), out xmlMap1, true,
        MessageBox.Show("The XmlMap could not be created");

void ThisWorkbook_BeforeXmlImport(Excel.XmlMap Map,
    string Url, bool IsRefresh, ref bool Cancel)
    if (DialogResult.No == MessageBox.Show("Microsoft Excel is about" +
        " to import XML into the workbook. Continue with importing?",
        "Custom XML Import Dialog", MessageBoxButtons.YesNo))
        Cancel = true;

void ThisWorkbook_AfterXmlImport(Excel.XmlMap Map, bool IsRefresh,
    Excel.XlXmlImportResult Result)
    if (Result == Excel.XlXmlImportResult.xlXmlImportSuccess)
        MessageBox.Show("XML import succeeded.");
        MessageBox.Show("XML import failed.");

Community Additions

© 2014 Microsoft