Partilhar via


WorkbookBase.XmlImport Method

Imports an XML data file into the current workbook.

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 Function XmlImport ( _
    url As String, _
    <OutAttribute> ByRef importMap As XmlMap, _
    overwrite As Object, _
    destination As Object _
) As XlXmlImportResult
public XlXmlImportResult XmlImport(
    string url,
    out XmlMap importMap,
    Object overwrite,
    Object destination
)

Parameters

  • url
    Type: System.String

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

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

  • destination
    Type: System.Object

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

Return Value

Type: Microsoft.Office.Interop.Excel.XlXmlImportResult
One of the XlXmlImportResult values.

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.

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 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
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);
}

.NET Framework Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace