Most Valuable Professional


View Jan Karel Pieterse's profile on LinkedIn subscribe to rss feed
Subscribe in a reader

Subscribe to our mailing list

* indicates required

Audit !!!

Check out our RefTreeAnalyser
the ultimate Excel formula auditing tool.

Trainings

Excel VBA Masterclass (English)
Excel VBA for Financials (Dutch)

Third party tools

Speed up your file

FastExcel
The best tool to optimise your Excel model!

Repair your file

Stellar Phoenix Excel Repair
Best tool to repair corrupt Excel sheets and objects
Home > English site > Articles > XML and Excel > XML Validation
Deze pagina in het Nederlands

XML and Excel

XML Validation

Odd enough, validation of XML files against their attached schema definition is turned off by default. Turning on this option will force Excel to check each XML file against the schema during import and export.

First an XML file containing a reference to s schema must be mapped to Excel cells. Close all open files in Excel you don't need and select File, Open. Select file test2-en.xml. I've left out the name of the first employee from this file. Again, select the option "Use the XML source task pane". Drag the branch "employee" to any cell. Next, click the properties button on the table tools tab of the ribbon (External table Data group), See fig. 12.

XML properties button on ribbon
Figure 12: XML properties button on ribbon

For Excel 2003, you can get at the properties dialog by clicking the appropriate button on the "List" toolbar:

The Properties button on the List toolbar in Excel 2003
Figure 13: The Properties button on the List toolbar in Excel 2003

The XML properties dialog opens:

XML Map properties dialog
Figure 14: XML Map properties dialog

Check the box next to "Validate data against schema for import and export".

 Now click the refresh button to import the data from test2_en.xml. The following dialog appears:

Error message caused by importing an XML file that violates its schema
Figure 15: Error message caused by importing an XML file that violates its schema

By clicking the Details... button Excel shows you a bit more information about what is wrong with your XML file (Figure 16). In this case Excel indicates that it did not expect the element "street" to appear yet, but the element "name" (apologies for the partially Dutch screenshot, a bug in Excel's multi-language pack). This error message is caused by the fact that I omitted the name tag and hence this tag is not in the expected position in the XML file. Despite the error message Excel did import the data.

Details of the validation errors caused by the offending XML file
Figure 15: Details of the validation errors caused by the offending XML file

Validation also works when exporting data. Open the file ExcelXML_en.xlsx and remove the name column entirely. Now try to export the XML list (see figure 16)

Rightclick cell menu, XML option, export
Figure 16: Rightclick cell menu, XML option, export

Error message after exporting XML data that violates the schema.
Figure 17: Error message after exporting XML data that violates the schema.


 


Comments

Showing last 8 comments of 13 in total (Show All Comments):

 


Comment by: Jan Karel Pieterse (3/17/2011 9:10:52 AM)

Hi Leisha,

I've seen it happen, but my memory fails me in why it occurs, sorry! Maybe it has to do with the structure of the map itself?

 


Comment by: Emma (12/16/2011 1:37:35 AM)

how do i write a code behind a button in vba to export an data in an excel worksheet to an xml file. pls its very urgent

 


Comment by: Jan Karel Pieterse (12/16/2011 3:45:20 AM)

Hi Emma,

If you insert a button from the Forms controls, you can attache a macro to it by right-clicking the button and selecting Assign Macro.

See www.jkp-ads.com/articles/controls.asp

 


Comment by: Emma (12/16/2011 9:42:10 AM)

pls i need the code to put behind the button in excel
i need to export the data in excel to an xml file. The xml file is supposed to act as a database so subsequent excel data can also be saved in it

 


Comment by: Jan Karel Pieterse (12/18/2011 11:04:47 PM)

Hi Emma,

The first thing I would do is to record a macro whilst manually importing the file. Then study that code and figure out where it needs to be modified to suit your needs.

 


Comment by: kpavan2004 (2/14/2014 9:10:51 PM)

I have a file which contains 10 xml messages.Each XML message is in single line. I would like to import the xml file into excel for analysis. Please suggest the correct approach to import the 10 xml messages into excel

 


Comment by: Jan Karel Pieterse (2/17/2014 7:04:50 AM)

Hi,

Have you tried importing the XML file already? What result do you get?

 


Comment by: alex (3/23/2014 9:42:34 AM)

Using an xml tool would actually be a much easier way of validating your xml and schema.

See; http://www.liquid-technologies.com/FreeXmlTools/FreeXmlValidator.aspx

 


Have a question, comment or suggestion? Then please use this form.

If your question is not directly related to this web page, but rather a more general "How do I do this" Excel question, then I advise you to ask your question here: www.eileenslounge.com.

Please enter your name (required):

Your e-mail address (optional but if you want me to respond it helps!; will not be shown, nor be used to send you unsolicited information):

Your request or comment:

To post VBA code in your comment, use [VB] tags, like this: [VB]Code goes here[/VB].