Share via


WorkbookBase.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.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Syntax

'Declaration
Public Sub SaveAsXMLData ( _
    filename As String, _
    map As XmlMap _
)
public void SaveAsXMLData(
    string filename,
    XmlMap map
)

Parameters

  • filename
    Type: System.String
    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.

Remarks

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.

Examples

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 is for a document-level customization.

Private Sub WorkbookXmlExportEvents()

    ' Create a new DataTable.
    Dim ds As New DataSet()
    Dim dt As DataTable = ds.Tables.Add("Customers")
    dt.Columns.Add(New DataColumn("LastName"))
    dt.Columns.Add(New DataColumn("FirstName"))

    ' Add a new row to the DataTable.
    Dim dr As DataRow = dt.NewRow()
    dr("LastName") = "Chan"
    dr("FirstName") = "Gareth"
    dt.Rows.Add(dr)

    ' Add a new XML map.
    Dim xmlMap1 As Excel.XmlMap = Me.XmlMaps.Add( _
        ds.GetXmlSchema())

    ' Import the data into Sheet1. 
    Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
    Me.XmlImportXml(ds.GetXml(), xmlMap1, True, _
        range1)

    If xmlMap1.IsExportable Then
        Me.SaveAsXMLData(Me.Name & ".xml", xmlMap1)
    End If
End Sub

Sub ThisWorkbook_BeforeXmlExport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByRef Cancel As Boolean) _
    Handles Me.BeforeXmlExport

    MsgBox("Microsoft Excel is exporting XML from " & _
        "the XmlMap.")
End Sub

Sub ThisWorkbook_AfterXmlExport(ByVal Map As Excel.XmlMap, _
    ByVal Url As String, ByVal Result As Excel.XlXmlExportResult) _
    Handles Me.AfterXmlExport

    If Result = Excel.XlXmlExportResult.xlXmlExportSuccess Then
        MsgBox("XML export succeeded.")
    Else
        MsgBox("XML export failed.")
    End If
End Sub
private void WorkbookXmlExportEvents()
{
    this.BeforeXmlExport +=
        new Excel.WorkbookEvents_BeforeXmlExportEventHandler(
        ThisWorkbook_BeforeXmlExport);

    this.AfterXmlExport +=
        new Excel.WorkbookEvents_AfterXmlExportEventHandler(
        ThisWorkbook_AfterXmlExport);

    // 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";
    dt.Rows.Add(dr);

    // Add a new XML map.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(
        ds.GetXmlSchema(), missing);

    // Import the data into Sheet1. 
    Excel.Range range1 = Globals.Sheet1.Range["A1", missing];
    this.XmlImportXml(ds.GetXml(), out xmlMap1, true,
       range1);

    // 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.");
    }
    else
    {
        MessageBox.Show("XML export failed.");
    }
}

.NET Framework Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace