Use Schemas with XML Maps

When Excel imports an XML file that does not reference an XML schema, it infers a schema from the nodes in the XML file. The preceding section explains one of the limitations of inferring a schema—optional nodes are sometimes omitted from the resulting XML map.

Another solution to this problem is to include a schema with your XML file. For example, the following XML fragment references a schema for the SimpleOrder.xml file:

<Orders xmlns=""
  xsi:schemaLocation=" SimpleOrder.xsd">

When Excel imports an XML file that references a schema, it copies that schema into the workbook. If the XML is valid according to that schema, you can drag nodes from the XML map onto the worksheet to create lists and import data as shown previously.

If the XML is not valid for the schema, however, no data will appear in the lists you create. Excel does not automatically validate XML against schemas or display errors if the XML is invalid. To validate XML within Excel:

  1. From the Data menu, choose XML then choose XML Map Properties. Excel displays the XML Map Properties dialog box (Figure 15-11).

  2. Select “Validate data against schema for import and export” and click OK to close the dialog box.

Validating XML

Figure 15-11. Validating XML

Now, Excel will display an error if the XML doesn’t conform ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.