ImportXml Method

Microsoft Office Web Components Visual Basic

ImportXml Method

When this method is called, the specified Spreadsheet component map is updated with an XML data stream. Returns Nothing.

expression.ImportXml(XMLData, ShowDialogs, UseIDXPath)

expression    An expression that returns an XmlMap object.

XMLData   Required Variant. A String or IStream object that contains the XML data to be imported.

ShowDialogs   Optional Boolean. Specifies whether a dialog box is displayed if the ImportXml method encounters an error that is returned by the Spreadsheet component itself. When ShowDialogs    is set to True, a dialog box is displayed if the ImportXml method encounters an error returned by the Spreadsheet component. When ShowDialogs is set to False, dialog boxes for Spreadsheet component errors are not displayed. The default is False.

UseIDXPath   Optional Boolean. Specifies how to import XML data into a Spreadsheet Web Part that implements an IRowConsumer connection interface in the RowConsumer element of its solution specification file. When set to False, the Spreadsheet Web Part imports the XML data according to the XPath statement specified in the XPath element within the Map element in the XML Spreadsheet file associated with the Web Part. When set to True, the Spreadsheet Web Part imports the XML data using a root/row schema (as shown below), and maps the row elements to the Field elements within the Map element of the XML Spreadsheet file associated with the Web Part. The default is False.

Root/Row Schema

      <Root>
    <Row>
        <Field1>Field1Value</Field1>
        <Field2>Field2Value</Field2>
    </Row>
</Root>
    

The Spreadsheet Web Part will attempt to match each field tag name (such as Field1 and Field2 in the schema example above) to the ID attribute of a Field element within the Map element of an XML Spreadsheet file. For example, the following XML fragment from an XML Spreadsheet file shows a Field element with an ID attribute value of "Field1".

<Map x2:ID="Products_Map" x2:SchemaID="Schema1" x2:RootElement="Products">
    <Entry x2:Type="table" x2:ID="2" x2:ShowTotals="false" x2:NoInserts="true">
    <Range>Products!R2C1:R4C8</Range>
    <HeaderRange>R1C1</HeaderRange>
    <x:FilterOn>True</x:FilterOn>
    <XPath>/Products/Products_Row</XPath>
    <Field x2:ID="Field1">
        <Range>RC</Range>
        <XPath>ProductID</XPath>
        <XSDType>int</XSDType>
    </Field>
    . . .

Note  The ShowDialogs argument does not control whether a dialog box will be displayed for errors external to the Spreadsheet component, such as errors returned by scripting components. To prevent a dialog box from displaying for errors external to the Spreadsheet component, you must trap those errors in your script.

Remarks

Use the ImportXml method to import the contents of an XML data file into cells or into an XML list mapped to a specific schema map. If the contents of the XML data file to be imported do not match the specified schema map and the ImportXml operation fails, an error dialog box will be displayed when the optional ShowDialogs argument is set to True.

The XML data being imported needs to "match" the XmlMap it's being imported into — that is, the XPaths contained in the XmlMap entries and fields should correspond to some element or attribute in the imported data.

During an asynchronous binding, all data-binding object model calls that include XmlMap.ImportXml will return an error message that says the requested operation cannot be completed because an asynchronous binding is in progress. Object model calls will start to succeed when the asynchronous binding is complete.

Example

This example shows how you can use the CanQuery property to first check whether a binding can be queried. If it can, the code refreshes the binding. An event hander traps the BindingCompleted event, which then calls the ExportXmlInfo function, passing another instance of the Spreadsheet component as an argument to that function. In the ExportXmlInfo function, the ExportXML method is called and the XML data stream is returned to the calling function and used as the input for the ImportXml method.

    Dim objBinding
Dim objBindings
Dim objXmlStringIn
Dim objXmlMap

Set objXmlMap = Spreadsheet1.ActiveWorkbook.XmlMaps.Item(1)
Set objBindings = Spreadsheet1.ActiveWorkbook.XmlDataBindings

For Each objBinding In objBindings
    If objBinding.CanQuery = True
        objBinding.Refresh
    End If
Next

Sub Spreadsheet1_BindingCompleted(bindingID, Action)

Dim objXmlStringIn
Dim objXmlMap

    ' Write out the data in the specified map to an XML data stream.
    objXmlStringIn = ExportXmlInfo(Spreadsheet2)

    ' Use the XML data stream by passing it as the argument to the ImportXml method.
    objXmlMap.ImportXml objXmlStringIn

End Sub


Function ExportXmlInfo(Spreadsheet2)
    Dim objXmlMap

    Set objXmlMap = Spreadsheet2.ActiveWorkbook.XmlMaps.Item(1)
    ' Return the map to the calling function.
    ExportXmlInfo = objXmlMap.ExportXml()
End Function

  

Note  For information on trapping the BindingCompleted event from script running in a Web page, see the BindingCompleted event topic.