ConvertCSV

How to Convert CSV to XML (Online and With Code)

By Convert CSV Editorial TeamLast updated August 1, 2026

Convert CSV to XML for partner imports and enterprise feeds. Learn browser steps, Python/Node examples, element naming, and how to avoid invalid markup.

Why Convert CSV to XML?

CSV is how analysts and spreadsheets store tabular data. Many enterprise systems still expect XML imports—product catalogs, EDI-style feeds, government portals, and legacy ERP modules. Converting CSV to XML bridges those worlds without rewriting the source spreadsheet.

What Good CSV → XML Looks Like

A clean conversion maps each CSV row to a repeating record element under a single root. Column headers become child element names (or attributes). Cell values become text content. Special characters must be escaped so the XML stays well-formed.

<?xml version="1.0" encoding="UTF-8"?>
<records>
  <record>
    <order_id>1001</order_id>
    <email>ada@example.com</email>
    <amount>42.50</amount>
  </record>
  <record>
    <order_id>1002</order_id>
    <email>grace@example.com</email>
    <amount>19.00</amount>
  </record>
</records>

Step-by-Step: In the Browser

The fastest path for one-off jobs.

  • Clean the CSV in the Online CSV Editor (headers, blank rows, encoding).
  • Open the CSV to XML Converter.
  • Upload or paste the CSV.
  • Confirm the root and record element names if the tool exposes them.
  • Download the .xml and open it in a text editor to spot-check structure.

Before you convert

Rename headers to valid XML names: letters, digits, underscores, hyphens. Avoid spaces and leading digits. Prefer snake_case or camelCase consistently.

Step-by-Step: With Python

ElementTree builds well-formed XML from DictReader rows.

import csv
import xml.etree.ElementTree as ET

root = ET.Element("records")

with open("orders.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        record = ET.SubElement(root, "record")
        for key, value in row.items():
            child = ET.SubElement(record, key)
            child.text = value or ""

tree = ET.ElementTree(root)
tree.write("orders.xml", encoding="utf-8", xml_declaration=True)

Pretty-print the output

Python 3.9+ supports indent for readable XML.

ET.indent(tree, space="  ")
tree.write("orders.xml", encoding="utf-8", xml_declaration=True)

Step-by-Step: With Node.js

Parse CSV, then build XML with a small builder or template.

import fs from "node:fs";
import { parse } from "csv-parse/sync";

const rows = parse(fs.readFileSync("orders.csv", "utf8"), {
  columns: true,
  skip_empty_lines: true,
  bom: true,
});

const escape = (s) =>
  String(s)
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;");

const body = rows
  .map((row) => {
    const fields = Object.entries(row)
      .map(([k, v]) => `    <${k}>${escape(v ?? "")}</${k}>`)
      .join("\n");
    return `  <record>\n${fields}\n  </record>`;
  })
  .join("\n");

fs.writeFileSync(
  "orders.xml",
  `<?xml version="1.0" encoding="UTF-8"?>\n<records>\n${body}\n</records>\n`,
);

Elements vs Attributes

Both are valid. Elements are easier to scan and extend. Attributes work well for short identifiers (id, sku, qty) when a partner schema demands them.

ChoiceExampleBest for
Child elements<email>ada@…</email>Most fields, long text
Attributes<item sku="A1" qty="2" />Short codes, schema-required attrs
Mix<order id="1001"><email>…</email></order>Common in real partner feeds

Escaping and Encoding

Invalid XML is worse than ugly XML. Escape &, <, >, and quotes in text. Use UTF-8 with an XML declaration. Strip or replace characters that are illegal in element names.

  • & → &amp;
  • < → &lt;
  • > → &gt;
  • " → &quot; (in attributes)
  • ' → &apos; (in attributes)

Real-World Examples

Where CSV → XML shows up weekly.

ERP product import

Merchandising keeps an Excel catalog, exports CSV, converts to the ERP’s XML template, and uploads overnight.

Government portal upload

A compliance team maintains a spreadsheet of filings, converts to schema-shaped XML, validates against XSD, then submits.

Partner B2B feed

Ops generates a nightly CSV from the warehouse, converts to XML, and drops it on an SFTP site.

Common Mistakes

These break partner imports more often than anything else.

  • Leaving spaces or special characters in column headers used as element names.
  • Forgetting to escape &, <, and > in cell values.
  • Mixing encodings so non-ASCII names corrupt.
  • Generating one giant document when the partner expects one file per batch size limit.
  • Skipping XSD validation after conversion.

Best Practices

Small habits keep feeds accepted.

  • Normalize headers to valid XML names first.
  • Always declare encoding="UTF-8".
  • Escape text content and attributes.
  • Match the partner’s root and record element names exactly.
  • Validate against XSD before upload when a schema exists.

Why Use Convert CSV Online?

Convert CSV Online is free, browser-based, and requires no account for everyday conversions. Clean the table in the Online CSV Editor, convert with CSV to XML, and reverse-check with XML to CSV if you need a spreadsheet preview. Client-side workflows work on Windows, macOS, and Linux browsers.

No install for one-off partner files

Perfect when you need a valid XML import once, not a permanent ETL job.

Conclusion

CSV to XML is a naming, escaping, and structure problem—not a mystery. Clean headers, escape content, match the partner schema, and validate before you upload.

FAQ

How do I convert CSV to XML?

Clean the CSV headers, then use Convert CSV Online’s CSV to XML Converter—or build XML in Python (ElementTree) or Node.js from DictReader/csv-parse rows.

What should element names be?

Use the CSV column headers after normalizing them to valid XML names: no spaces, no leading digits, consistent snake_case or camelCase.

Do I need to escape special characters?

Yes. Escape &, <, >, and quotes so the XML stays well-formed. Libraries often do this for you; string templates do not.

Can I put CSV columns into XML attributes?

Yes. Map short identifiers (id, sku, qty) to attributes when the partner schema requires them; keep longer text as child elements.

How do I validate the XML after conversion?

Validate against the partner’s XSD with xmllint, an IDE, or a schema-aware library before uploading.

Is browser conversion safe for sensitive data?

Convert CSV Online processes files in your browser for everyday conversions, so sensitive rows stay on your machine.

References

Convert your CSV in the browser

Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.