CSV to XML: How to Convert a CSV File to Valid XML Online
Convert CSV to XML online for free: map the header row to element names, set root and row tags, fix invalid names, escape values and pick the right encoding.
Updated November 8, 20269 min read
To convert CSV to XML, open the CSV to XML converter, drop your file, name the root element and the row element, check the preview and download. Each row of the CSV becomes one row element, and each column becomes a child element named after the header, or an attribute if you prefer. The part that takes thought is not the conversion itself but the gap between the two formats: a spreadsheet header can be any text at all, while an XML name has strict rules, and characters such as & and < must be escaped. This guide covers how the mapping works, what the settings do and the specific things that break an import.
How a CSV to XML converter maps your columns
Take a three-column file with the header id,city,total and one data row 7,Paris,19.90. With the default settings the result is a root element called root, one element called row per data line, and one child element per column: <root><row><id>7</id><city>Paris</city><total>19.90</total></row></root>. The first line of the CSV is never written out as data; it supplies the names.
| CSV part | Becomes in XML | Setting that controls it |
|---|---|---|
| The whole file | One root element wrapping everything | Root element (default root) |
| Each data line | One element inside the root | Row element (default row) |
| Each header field | The name of a child element or attribute | First row as element names |
| Each cell | Element text, attribute value or CDATA | Write columns as, plus per-column overrides |
| An empty cell | An empty element, nothing, or xsi:nil | Empty cells |
| No header row | Names column_1, column_2 and so on | Turn First row as element names off |
Before choosing anything, ask what will read the file. If another system expects a particular shape (say <products><product> with a sku attribute), set the names to match exactly; XML is case-sensitive, so Product and product are different elements and an importer looking for one will not find the other.
Converting CSV to XML, step by step
Add the CSV
Open the CSV to XML tool and drop a .csv, .tsv, .txt or .psv file. The delimiter (comma, semicolon, tab or pipe), the quote character and the encoding are detected from the file.
Name the root and row elements
Replace root and row with the names the receiving system expects, for example orders and order. If you type a name XML will not accept, the tool warns you and repairs it before use.
Choose how columns are written
Under Write columns as, pick Elements or Attributes for the whole file. In the Columns table you can then set any single column to CDATA or Skip, and you can see each header next to the element name that will be used.
Decide on empty cells and the declaration
Pick Empty element, Omit or xsi:nil for blank cells, and leave Include XML declaration on unless the receiver asks for a bare fragment.
Check the preview and download
The preview shows the first twenty rows built by the same code as the export, with a Well-formed or Not well-formed badge. When it looks right, press Download XML.
Header names that XML will not accept
This is where most hand-made conversions fail. An XML element name must start with a letter or an underscore, and it may not contain spaces, most punctuation or a colon. Names beginning with xml, in any capitalisation, are reserved by the specification. Spreadsheet headers break these rules all the time: Order ID, 2024, -id, Total (£), Unit price/kg. A converter that copies them straight into tags produces a file that every parser rejects on the first line.
| Header in the CSV | Element name used | Why |
|---|---|---|
| First Name | First_Name | A run of illegal characters becomes one underscore |
| Total (£) | Total | Trailing punctuation is dropped, not turned into underscores |
| 2024 | _2024 | A name cannot start with a digit, so an underscore is added |
| xml or XmlData | Prefixed with an underscore | Names starting with xml are reserved |
| Name, Name | Name, Name_2 | Duplicate names are numbered apart |
| Führung, 名前 | Unchanged | Letters from any script are legal in XML |
The tool repairs every name to the XML naming rules and shows the original header beside the name it used, with a count of how many were renamed, so nothing changes without you seeing it. If the receiving system needs specific names, the cleanest fix is to rename the headers in the CSV itself before converting, because then the file you keep and the XML you send agree.
Root and row names, elements or attributes per column, CDATA, empty-cell rules, indentation and encodings are all free. Free: one file up to 25 MB. Pro: up to 50 files at once in one ZIP, nesting by a column, namespaces and a generated XSD schema. It all runs on your device, not our servers.
Convert CSV to XMLElements, attributes or CDATA?
Elements are the safe default. <city>Paris</city> can hold text of any length, including line breaks, can be empty, and can carry a type. Choose them when you are not sure.
Attributes put the value on the row itself: <row id="7" city="Paris"/>. The file is noticeably smaller and reads well for short values like ids and codes. Many importers expect ids as attributes and descriptive fields as elements, which is why the default can be overridden per column. One trap: XML parsers replace a raw tab or newline inside an attribute value with a space, so a multi-line address stored as a plain attribute loses its line breaks. This converter writes those characters as references ( for a newline) so the value comes back intact.
CDATA wraps a value so it passes through unchanged, which suits a column holding an HTML snippet. Skip leaves a column out entirely, useful for internal keys that should not travel to the other system.
Escaping: the characters that break XML
Five characters have a special meaning in XML. In element text, & becomes &, < becomes < and > becomes >. Inside an attribute, double and single quotes are escaped as well, as " and '. A product name like Fish & Chips <large> must arrive as Fish & Chips <large> or the file is not well-formed. The converter does this on every value, so you should not pre-escape anything in the CSV; if you do, & turns into &amp; and the receiver shows the code instead of the character.
Some characters cannot appear in XML 1.0 at all, escaped or not: most control characters below the space, such as the NUL byte or the form feed that old database exports sometimes contain. They are removed and the tool reports how many. If that count is large, the source export is probably damaged or in the wrong encoding.
A CSV with broken quoting (a stray quote mark in the middle of a field) can merge several lines into one long cell, and no amount of XML escaping fixes that. Run it through CSV Escape / Fix first, or open it in the CSV viewer to see whether the columns line up.
Encodings and the accents that turn into junk
The XML file is always written as UTF-8, and the declaration line says so: <?xml version="1.0" encoding="UTF-8"?>. The risk is on the way in. If the CSV is read with the wrong encoding, café turns into café or a question mark, and that damage is carried into the XML as if it were real text.
| The CSV came from… | Encoding to use | Sign you picked wrong |
|---|---|---|
| Most modern apps and online exports | UTF-8 (Auto finds it) | é or ’ in place of accents and apostrophes |
| A Unicode Text export from a spreadsheet | UTF-16 LE (a byte-order mark usually decides it) | One single column full of gaps |
| An older Western-European system or a default spreadsheet save | Windows-1252 | Euro signs and curly quotes become boxes |
| A file that really is Latin-1 | ISO-8859-1 (true) | Rare; bytes 0x80 to 0x9F are control codes, not quotes |
Leave Encoding on Auto-detect first. If the file starts with a byte-order mark, the mark always wins over the dropdown and the tool tells you it ignored your choice. Check a row with accented names in the preview before downloading.
Settings that change what the receiver sees
- Empty cells: Empty element writes <city></city>, Omit writes nothing, and xsi:nil writes <city xsi:nil="true"/>, the only one that tells a schema-aware system the value is known to be absent.
- Add xsi:type to elements: labels each value as an integer, decimal, boolean, date, time, dateTime or string, but only when every value in the column fits. A postal code such as 01234 stays a string so its leading zero survives.
- Indentation: 2 spaces, 4 spaces or a tab for a file people will read; Minified puts everything on one line for the smallest file.
- Skip leading rows and Comment prefix: drop a title line above the real header, or lines starting with #, so they are not taken as names or data.
If your file is larger than 25 MB, split the CSV by row count and convert the parts, keeping the header on each one. If you are still deciding whether XML is the right target at all, JSON, CSV or XML compares the three formats; for data that is already JSON, use JSON to XML instead.
Frequently asked questions
How do I convert a CSV file to XML for free?
Drop the CSV onto the CSV to XML converter, set the root and row element names, choose elements or attributes, check the twenty-row preview and download the XML. One file up to 25 MB is free, with every setting that affects the XML itself included.
How do I use the CSV header row as XML tags?
Keep First row as element names switched on. Each header becomes the name of a child element, repaired where XML would reject it, for example 2024 becomes _2024. Switch it off and the columns are named column_1, column_2 and so on.
Why does my XML say it is not well-formed?
Usually a tag name that breaks XML naming rules, an unescaped & or < in a value, or a stray control character. The converter repairs names, escapes values and removes illegal characters, so its own output is well-formed. If a file you made elsewhere fails, convert the CSV again here.
Can I make some columns attributes and others elements?
Yes. Set the default under Write columns as, then change any single column in the Columns table to Elements, Attributes, CDATA or Skip. All of this is free.
Why do accented letters look wrong in my XML?
The CSV was read with the wrong encoding. The XML is written as UTF-8, so the damage happened on input. Set Encoding to UTF-8, Windows-1252 or UTF-16 to match how the CSV was saved, and check a row with accents in the preview.
Can I convert several CSV files or create nested XML?
Those are Pro features: up to 50 files at once downloaded as one ZIP, grouping rows under a key column for nested XML, a namespace, and a generated XSD schema. The free version converts one file at a time into flat XML.