CSV vs XML

CSV vs XML explained: how each format stores data, what changes when you convert between them, and how to check the output will work where you need it.

CSV and XML are both plain-text formats for storing and exchanging data, but they are built for different data shapes. CSV holds flat rows and columns. XML holds a tree of named, nested elements. This guide explains how the two differ, what happens to your data when you convert between them, and how to check that the converted output will work where you plan to use it.

How each format represents data

CSV (comma-separated values) stores one record per line and separates fields with a delimiter, usually a comma. An optional first line holds column names. A field that contains the delimiter, a double quote or a line break is wrapped in double quotes, and a literal double quote inside it is written twice. CSV has no built-in data types, no nesting and no single binding standard. A widely referenced specification exists, but real-world files vary in delimiter, quoting, line endings and encoding. Tab-separated values (TSV) follow the same row-and-column model with a tab as the delimiter.

XML (Extensible Markup Language) wraps each value in named tags. Elements can contain other elements, so the format can describe hierarchies, repeated groups and optional parts. Values can also be stored as attributes on an element. A document must be well-formed: tags must be properly nested and closed, and characters such as < and & must be escaped. XML can declare its character encoding. Schemas such as DTD or XML Schema can define and validate the expected structure and data types, although a schema is optional.

The same data in both formats

The two snippets below are illustrative. They were written by hand to show the formats and were not produced by any particular converter. Both hold the same two records.

sku,name,qty
00412,"Lamp, brass",3
00413,"Cable ""flat"" 2 m",12
<?xml version="1.0" encoding="UTF-8"?>
<items>
  <item sku="00412">
    <name>Lamp, brass</name>
    <qty>3</qty>
  </item>
  <item sku="00413">
    <name>Cable "flat" 2 m</name>
    <qty>12</qty>
  </item>
</items>

The CSV version is compact, but its meaning depends on the header row and the quoting rules. The XML version labels every value and is longer. Some choices in the XML were made by the author and are not part of the data itself: the names items and item, and storing sku as an attribute rather than as a child element. Another converter could make different choices and still produce valid XML.

What converting between them changes

Converting CSV to XML keeps the values but adds structure that someone must define. Someone has to choose the root element name, the row element name, and whether each column becomes a child element or an attribute. Column headers may be renamed, because XML names cannot contain spaces or start with a digit. Characters such as & and < are escaped in the output. Every value is still text unless a schema assigns it a type. The output is larger, and it does not gain any nesting the original data lacked.

Converting XML to CSV usually means flattening the tree. Repeated or nested children have no natural place in a single row. In the illustrative order below, the two line elements become two rows, and the order ID and customer are repeated on each row.

<order id="A1">
  <customer>Ana Rivera</customer>
  <line sku="00412" qty="3"/>
  <line sku="00413" qty="12"/>
</order>

order_id,customer,sku,qty
A1,Ana Rivera,00412,3
A1,Ana Rivera,00413,12

Converters handle this differently. Some repeat the parent fields on each row, as shown above. Some join repeated values into one cell with a secondary separator. Some write separate files linked by a key. Some drop or truncate repeated values. The flattening can also lose information: the distinction between attributes and elements, namespaces, comments, processing instructions and text mixed with child elements. Unless the mapping is recorded, the original XML usually cannot be rebuilt from the CSV.

Side-by-side comparison

Criterion | CSV | XML
Data shape | Flat rows and columns | Nested tree of elements and attributes
Labels | Column names in an optional header row | Every value wrapped in named tags
Data types | None; all values are text | Text by default; types can be defined by a schema
Validation | No standard mechanism | Well-formedness is required; schema validation is optional
Size for the same flat data | Smaller | Larger, because tags repeat for every value
Human scanning | Easy for small tables | Readable, but verbose
Editing | Spreadsheets and text editors | Text editors or XML-aware tools
Repeated or optional groups | Need workarounds | Supported directly
Round trip to the other format | CSV to XML is usually recoverable if the mapping is known | XML to CSV often loses structure

Which format suits your task

  • Choose CSV when the data is naturally a single table, the recipient works in spreadsheets or loads tabular data into a database, file size matters, or simple and widely supported import matters more than strict validation.
  • Choose XML when records contain nested or repeating parts, the receiving system requires XML or a specific schema, you need to validate structure before accepting data, or values need metadata such as attributes, namespaces or language tags.

The main tradeoff is simplicity versus expressiveness. CSV is easy to produce and inspect, but it relies on unwritten conventions about delimiters, quoting and meaning. XML makes structure explicit and checkable, but it is verbose and needs more tooling. If the destination system names a format, use that format. Otherwise, let the shape of the data decide: flat data fits CSV, and hierarchical data fits XML.

Will the output stay usable where you need it?

Spreadsheet import. When spreadsheet applications open CSV files, they commonly try to detect data types. This can strip leading zeros (00412 becomes 412), turn long numeric IDs into rounded or scientific notation, and reformat values that look like dates. To keep a column as text, use the application's import dialog and set the column type explicitly instead of opening the file directly. Check the result in your own application, because the behavior depends on the program and its settings.

Delimiters and locale. In regions where the comma is the decimal separator, some software writes or expects semicolon-delimited files under the CSV name. A file that looks correct on one machine can load as a single column on another. Confirm which delimiter the destination expects.

Encoding. CSV has no way to declare its own encoding. Non-ASCII characters can appear garbled if the reader assumes a different encoding than the writer used. Some tools expect a byte order mark at the start of a UTF-8 file, and others treat that mark as stray characters in the first header. XML can declare its encoding in the opening declaration. The declaration only helps if it matches the actual bytes in the file.

Strict parsing. A conforming XML parser stops at the first well-formedness error. One unescaped & or one unclosed tag can make the whole file unreadable to it. CSV readers are usually more forgiving, but that can hide problems: a stray quote or an unquoted line break can shift every later field into the wrong column without any error message.

Copy and paste. Converted text survives the clipboard as ordinary characters, but how the destination interprets it varies. Pasting CSV into a spreadsheet may land in one column unless the application splits it. Tab-separated text is often split into cells on paste, but this depends on the application. Long lines may wrap in chat tools or editors, and some tools convert straight quotes to curly quotes. Either change can break the file. For exchange, send a file attachment rather than pasted text.

Accessibility. Neither format is meant for presentation. A screen reader reading raw CSV announces the commas and quotes, and raw XML announces the markup. If people need to read the data, render it as a table with header cells, or as a properly structured document, rather than sharing the raw file.

Security and sensitive data. Spreadsheet applications can treat CSV cells that begin with =, +, - or @ as formulas, which can run unintended actions when an untrusted file is opened. Neutralize such values before sharing exported data. Parsing untrusted XML with external entities or DTD processing enabled can expose local files or exhaust memory, so disable those features unless you need them. If you use an online converter, the data leaves your control once you upload it. For personal, financial or confidential records, prefer a local tool or script, or confirm the service's data-handling terms first.

  • Before relying on converted output, check:
  • The destination's expected format, delimiter, encoding and, for XML, any required schema.
  • That one sample row or record round-trips with leading zeros, special characters and line breaks intact.
  • How repeated XML elements were flattened, and whether any values were dropped.
  • That the XML output passes a well-formedness check, and schema validation if one applies.
  • That no CSV cells start with formula-trigger characters when the file will be opened in a spreadsheet.

Gaps in this comparison

This guide is based on general properties of the two formats. It does not test specific converters, spreadsheet versions or parsers, so the examples above are illustrative rather than observed output. No measured file sizes, conversion speeds or tool compatibility results are included. These depend on your data and software, so test with a representative sample before converting production files.