Choose between XML and CSV

XML or CSV? A practical comparison of structure, readability, copy and paste behavior, and conversion risks, with guidance on when each format fits.

The short answer

Choose CSV when your data is a single flat table: one record per row, the same fields in every row, and a destination such as a spreadsheet, database import or simple script. Choose XML when the data has nesting, repeated child items, optional or mixed structures, or when the receiving system needs a declared schema, namespaces or embedded metadata. The deciding tradeoff is that CSV is compact and easy to open but holds only rows and columns, while XML can hold almost any structure but is more verbose and needs a parser to work with comfortably.

If neither a program nor a spreadsheet will consume the data, and people only need to read it, a plain-text summary or a formatted table is often clearer than either format.

How the two formats differ

CSV (comma-separated values) is a line-based text format. Each line is a record, and a delimiter separates the fields. The widely cited convention, RFC 4180, wraps a field in double quotes when it contains a comma, a quote or a line break, and writes a literal quote as two quotes. Many programs depart from that convention, so CSV in practice is a family of similar dialects rather than one strict format. TSV is the same idea with a tab as the delimiter. It avoids most quoting, but it breaks if a value contains a tab or a line break.

XML (Extensible Markup Language) is a W3C-specified markup format. Data sits inside named elements that can contain other elements, and those elements can carry attributes. A parser must reject a document that is not well formed, for example one with unclosed tags. A document can declare its character encoding and can be validated against a schema.

Here is the same small dataset in both formats. In the CSV version, the comma and the quotation marks inside values need quoting. In the XML version, the ampersand needs escaping.

id,name,note
001,"Smith, Jr.","Said ""hi"""
002,O'Neil,R&D
<records>
  <record id="001">
    <name>Smith, Jr.</name>
    <note>Said "hi"</note>
  </record>
  <record id="002">
    <name>O'Neil</name>
    <note>R&amp;D</note>
  </record>
</records>

What converting between them actually changes

Converting between XML and CSV rewrites the stored characters. It is not a display effect, so the result behaves differently in every program that opens it. Three kinds of change matter most:

  • Structure. CSV to XML wraps each row and field in elements. The converter, or you, must choose element names and decide whether values become elements or attributes. XML to CSV must flatten the hierarchy into columns.
  • Escaping. CSV quoting rules are swapped for XML entity escaping, such as & and <, or the reverse. A converter that skips this step produces broken output.
  • Types and meaning. Neither format records whether 001 is a number or a text code. CSV has no field for this at all. XML can carry the information only if a schema or a convention supplies it.

CSV to XML can usually preserve every value, because a flat table fits easily inside XML. XML to CSV is where meaning is most often lost. Take an order with several line items. It has to be split into repeated rows, joined into one delimited cell, or spread over several files. Attributes, namespaces, comments, processing instructions and mixed text-and-markup content often have no natural column. Before you rely on a converter, check how it handles these cases.

<order id="A1">
  <customer>Lee</customer>
  <item sku="X1" qty="2"/>
  <item sku="Y7" qty="1"/>
</order>

order_id,customer,sku,qty
A1,Lee,X1,2
A1,Lee,Y7,1

In the flattened version, the order and customer values repeat on every row. That is a common and workable choice, but the file no longer states that the two rows belong to a single order. Whatever reads the file has to infer that from the repeated ID.

Readability and usability after copying or sharing

  • Spreadsheets. Opening or pasting CSV into a spreadsheet is convenient, but spreadsheet programs may reinterpret values. Leading zeros can disappear, long numbers can turn into scientific notation, and date-like strings can become dates. Import settings that treat columns as text usually prevent this. Behavior varies by program and version.
  • Delimiters and locale. In some regional settings, spreadsheets expect a semicolon instead of a comma, so a file that opens cleanly on one machine may land in a single column on another.
  • Encoding. CSV has no built-in place to declare its character encoding, so accented letters and non-Latin scripts can turn into garbled characters when the receiver guesses wrong. XML can declare its encoding, and a conforming parser should honor the declaration.
  • Copy and paste of XML. Pasting XML into chat apps, email or word processors can introduce curly quotes, strip indentation or wrap lines. Curly quotes in attribute values make the document invalid. Send XML as a file or inside a code block rather than as formatted text.
  • Human reading and assistive technology. Small CSV files open as an ordinary table that people can scan. Raw XML is harder to read, and a screen reader will typically announce the tags and punctuation along with the data. No assistive-technology testing was available for this article, so if accessibility matters, test with the specific screen reader and viewer your audience uses.
  • Search and editing. Both formats are plain text, so text search works. Editing CSV by hand risks misaligning columns or breaking quoting. Editing XML by hand risks unclosed tags, which a strict parser will reject.

When the goal is to share findings with people rather than feed a program, attach a plain-text or rendered-table version alongside the data file. Raw markup and long delimited lines are hard to read on small screens and with assistive technology.

Safety points in a conversion workflow

  • CSV formula injection. A cell that begins with =, +, - or @ may be treated as a formula when the file is opened in a spreadsheet. If the data comes from untrusted sources, neutralize those leading characters or import columns as text.
  • XML external entities. Some XML parsers resolve external entities by default, which can expose local files or trigger network requests when a parser reads untrusted documents. Disable entity and DTD processing unless you need it.
  • Online converters. A converter's appearance or marketing says nothing reliable about what happens to uploaded data. For sensitive files, prefer a local tool or review the provider's documented retention policy.

Which format fits your need

Criterion | CSV | XML
Data shape | One flat table | Nested, repeated or optional structures
Human scanning of small files | Easy in a spreadsheet or text editor | Harder; tags add visual noise
File size for the same data | Smaller, since there are no tags | Larger, because element names repeat
Declared encoding | Not built in | Supported in the XML declaration
Validation | No standard schema mechanism | Schema languages available
Spreadsheet import | Direct, with value-reinterpretation risks | Possible in some tools, less straightforward
Strictness | Loose; many dialects in use | Must be well formed or parsers reject it
Typical risks | Delimiter, quoting and encoding mismatches; formula injection | Verbosity; parser configuration such as external entities

The table summarizes general format properties. It is not a measured comparison of specific tools, file sizes or speeds; no such tests were available for this article. Results depend on the data and on the converter used.

  • Pick CSV if your data fits one table, recipients will use spreadsheets or bulk-import tools, and compactness matters.
  • Pick XML if the data has hierarchy, a partner system or industry standard requires it, or you need validation and a declared encoding.
  • Pick TSV over CSV if values often contain commas but never tabs or line breaks.
  • Consider another structured format if you need nesting but find XML too verbose and the destination accepts alternatives; check that the destination supports it first.
  • Use plain text or a rendered table if people, not programs, are the main readers.

Whichever format you choose, test a round trip on a small sample that contains commas, quotes, line breaks, leading zeros and non-ASCII characters. Open the result in the actual destination before you convert the full dataset.