Share via


WorkbookBase.XmlImport(String, XmlMap, Object, Object) Method

Definition

Imports an XML data file into the current workbook.

public Microsoft.Office.Interop.Excel.XlXmlImportResult XmlImport (string url, out Microsoft.Office.Interop.Excel.XmlMap importMap, object overwrite, object destination);
member this.XmlImport : string * XmlMap * obj * obj -> Microsoft.Office.Interop.Excel.XlXmlImportResult
Public Function XmlImport (url As String, ByRef importMap As XmlMap, Optional overwrite As Object, Optional destination As Object) As XlXmlImportResult

Parameters

url
String

A uniform resource locator (URL) or a uniform naming convention (UNC) path to an XML data file.

importMap
XmlMap

The schema map to apply when importing the file.

overwrite
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.

destination
Object

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

Returns

One of the XlXmlImportResult values.

Examples

The following code example creates an XmlMap based on a schema generated from a DataSet, writes the DataSet to an XML file, and then uses the XmlImport method to write the data in the XML file to a range in worksheet Sheet1.

This example is for a document-level customization.

private void WorkbookXmlImport()
{
    // 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 and write the XML to a file.
    Excel.XmlMap xmlMap1 = this.XmlMaps.Add(ds.GetXmlSchema(),
        "NewDataSet");
    ds.WriteXml(@"C:\Customers.xml");

    // Import the XML from the file.
    Excel.Range range1 = Globals.Sheet1.Range["A1"];
    this.XmlImport(@"C:\Customers.xml", out xmlMap1, true, range1);
}
Private Sub WorkbookXmlImport()

    ' 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 and write the XML to a file.
    Dim xmlMap1 As Excel.XmlMap = Me.XmlMaps.Add(ds.GetXmlSchema(), _
        "NewDataSet")
    ds.WriteXml("C:\Customers.xml")

    ' Import the XML from the file.
    Dim range1 As Excel.Range = Globals.Sheet1.Range("A1")
    Me.XmlImport("C:\Customers.xml", xmlMap1, True, _
        range1)
End Sub

Remarks

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 XmlImportXml method to import XML data that has been previously loaded into memory.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Applies to