Export (0) Print
Expand All
Expand Minimize

Workbook.SaveAsXMLData Method

Exports the data that has been mapped to the specified XML schema map to an XML data file.

Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel (in microsoft.office.tools.excel.dll)

public virtual void SaveAsXMLData (
	string Filename,
	XmlMap Map



The name of the file to be saved. You can include a full path; if you do not, Microsoft Office Excel saves the file in the current folder.


An XmlMap object. The schema map to apply to the data.

XML features, except for saving files in the XML Spreadsheet format, are available only in Microsoft Office Professional Edition 2003 and Microsoft Office Excel 2003.

This method will result in a run-time error if Excel cannot export data with the specified schema map. To check whether Excel can use the specified schema map to export data, use the IsExportable property.

The following code example demonstrates how to export XML data from a workbook. The example imports the XML data from a DataSet into a XmlMap in the current workbook. The example then calls the SaveAsXMLData method to export the data from the XmlMap into an XML file. When the SaveAsXMLData method is called, the BeforeXmlExport event handler reports that the XML is being exported, and the AfterXmlExport event handler reports whether the XML was successfully exported.


This example assumes that the ExcelLocale1033Attribute attribute is true (the default). If the ExcelLocale1033Attribute attribute is false, you can call the XmlImportXml method of the Microsoft.Office.Tools.Excel.Workbook host item, and you do not need to use the Unwrap method. For more information, see the XmlImportXml topic.

private void WorkbookXmlExportEvents()
    this.BeforeXmlExport +=
        new Excel.WorkbookEvents_BeforeXmlExportEventHandler(

    this.AfterXmlExport +=
        new Excel.WorkbookEvents_AfterXmlExportEventHandler(

    // 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.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(
        ds.GetXmlSchema(), missing);

    // Get objects that can be used while the ExcelLocale1033 
    // attribute is true (the default).
    Excel.Workbook workbook1 = (Excel.Workbook)Microsoft.Office.Tools.
    Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
    range1 = (Excel.Range)Microsoft.Office.Tools.Excel.
    xmlMap1 = (Excel.XmlMap)Microsoft.Office.Tools.Excel.

    // Import the data into Sheet1. 
    workbook1.XmlImportXml(ds.GetXml(), out xmlMap1, true,

    // Export the data.
    if (xmlMap1.IsExportable)
        this.SaveAsXMLData(this.Name + ".xml", xmlMap1);

void ThisWorkbook_BeforeXmlExport(Excel.XmlMap Map, string Url,
    ref bool Cancel)
    MessageBox.Show("Microsoft Excel is exporting XML from " +
        "the XmlMap.");

void ThisWorkbook_AfterXmlExport(Excel.XmlMap Map, string Url,
    Excel.XlXmlExportResult Result)
    if (Result == Excel.XlXmlExportResult.xlXmlExportSuccess)
        MessageBox.Show("XML export succeeded.");
        MessageBox.Show("XML export failed.");

© 2014 Microsoft