Data Import & Export

Import XML Files into Excel with the Source File Name

Rana GarciaRana Garcia Nov 27, 2026 868 views

Question details

Combine thousands of XML files into one Excel table and identify the source file for every imported record.

Import XML Files into Excel with the Source File Name
Product
Microsoft Excel Power Query
Device & OS
not provided
Scenario
The XML files may have different field counts and field orders, and some files may be empty.
Observed behavior
The desired table contains all available records aligned by field name, with a source file name on every row.
Before you start

Place the XML files in a known folder and inspect a nonempty file to identify the XML node that contains the records. Check that your Excel version supports Power Query and that the combined result fits within the worksheet row limit.

Solution 1Recommended

Combine XML files by field name with Power Query

Use a folder query to add each file name to its records and combine tables with different field sets.

The query below uses XML.Tables to read each file. If that function returns a wrapper or nested table rather than the actual records, adjust the TransformXml function to navigate to the record table for your XML structure before combining the files.

Table.Combine aligns columns by name and fills missing fields with null. The example skips files that cannot be parsed or return no rows, so review skipped files separately if you need to distinguish empty files from malformed XML.

1
Inspect a sample XML file

In Excel, select Data > Get Data > From File > From XML and choose a nonempty sample. In Power Query, inspect the nested tables to find the level where one row represents one record and the fields are columns.

2
Create a blank query

Select Data > Get Data > From Other Sources > Blank Query, then open Home > Advanced Editor.

3
Enter the folder query

Replace the editor contents with the following code and change the folder path. If necessary, replace XML.Tables(content) inside TransformXml with the navigation steps identified in your sample query: let TransformXml = (content as binary) as table => XML.Tables(content), Source = Folder.Files("C:\Your\XML\Folder"), XmlFiles = Table.SelectRows(Source, each Text.Lower([Extension]) = ".xml"), WithData = Table.AddColumn(XmlFiles, "Data", each let fileName = [Name], records = try TransformXml([Content]) otherwise null in if records = null then null else Table.AddColumn(records, "Source XML File", each fileName, type text)), Nonempty = Table.SelectRows(WithData, each [Data] <> null and Table.RowCount([Data]) > 0), Tables = Nonempty[Data], Combined = if List.IsEmpty(Tables) then #table({}, {}) else Table.Combine(Tables) in Combined

4
Check and load the results

Select Done and inspect the preview. Confirm that each record has a value in Source XML File and that fields align correctly, then select Home > Close & Load.

5
Refresh after files change

Add new XML files to the folder, then select Data > Refresh All in Excel to run the query again.

Combine XML files by field name with Power Query
Check the XML structure: A heading-only XML file may still produce a row at a wrapper level. Navigate to the actual record table and filter any non-record rows before loading. The source data must not already contain a column named Source XML File unless you change that name in the query.
Free Microsoft Office alternative

Review Combined XML Data in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar interface. After combining the XML files in Excel, open the resulting workbook in WPS Spreadsheets to review and work with the table.

Review imported records and their source file names in a spreadsheetCompatible with Microsoft Excel XLSX and XLS files for easier migrationFamiliar spreadsheet interface for working with tablesFree and lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Can Excel hold 240,000 records with 59 fields?

Yes. An Excel worksheet supports up to 1,048,576 rows and 16,384 columns. Allow one row for headers and check that your computer has enough memory to process the files.

What happens when XML files have different fields or field orders?

Table.Combine matches columns by name rather than position and fills missing columns with null. First make sure TransformXml returns each file's records as a flat table with consistent field names.

Why might an empty or invalid XML file be skipped?

The query uses try otherwise null for files that cannot be parsed and filters out tables with no rows. This also skips malformed files, so inspect the folder separately if you need to identify errors.

Why does the preview show nested tables instead of my records?

XML.Tables reflects the XML hierarchy. Open a sample through Data > Get Data > From File > From XML, navigate to the table containing the records, and use those navigation steps in the TransformXml function.

Does the folder query include files in subfolders?

Yes. Folder.Files includes files in the specified folder and its subfolders. Use Folder.Contents instead if you only want to work with entries in the specified folder, and adjust the query to select files.