How to open an XML file without breaking it
Opening an XML file is easy. Opening it in something that will not quietly corrupt it is the part nobody warns you about. Here is what to use on each platform, and the two apps to keep away from it.
An XML file is plain text — the format is defined by the W3C XML specification — so almost anything can open it. That is exactly the problem — several of the apps most likely to grab it will also change it, and some of those changes are invisible until a parser three systems downstream rejects the file.
So this is less "how do I open it" and more "what do I open it with so I do not regret it".
First: do not let these two near it
If you only need to look, opening in Word technically works. The danger is the reflex of hitting Ctrl+S on the way out.
The easiest option: your browser
Drag the file onto a Chrome, Edge, or Firefox window. Browsers ship a real XML parser and give you a collapsible tree with syntax colouring, for free, with nothing installed.
There is a bonus here that people miss: the browser will refuse to render malformed XML. Instead of a tree you get an explicit parse error with a line and column. That makes a browser a perfectly good first-pass validator.
This page contains the following errors:
error on line 14 at column 9: Opening and ending tag mismatch: plan line 12 and subscriber
Below is a rendering of the page up to the first error.That message tells you the tags are crossed — something opened inside <plan> and closed as if it were <subscriber>. Line 14, column 9. That is a far better starting point than "invalid file".
Windows
- VS Code — the one to actually install. Syntax highlighting, tag folding, and it will flag mismatched tags as you scroll.
- Notepad++ — lighter. Add the XML Tools plugin, then Plugins → XML Tools → Pretty print to expand a single-line file.
- Notepad — safe, in that it will not alter anything, but a minified XML file will appear as one enormous line.
- Your browser — as above, and nothing to install.
macOS
TextEdit works but will try to be clever; put it in plain-text mode first (Format → Make Plain Text). The command line is quicker:
# Pretty-print a dense file (xmllint ships with macOS)
xmllint --format subscribers.xml
# Check whether it is well-formed, without printing it
xmllint --noout subscribers.xml && echo "well-formed"
# Validate against a schema you were given
xmllint --noout --schema subscribers.xsd subscribers.xmlThat second command is the one worth remembering. --noout suppresses the document and prints only errors, so silence means the file is fine.
Linux
# Same tool, usually via libxml2-utils
sudo apt install libxml2-utils
xmllint --format subscribers.xml | less
# XPath straight from the shell — pull one field out of a large file
xmllint --xpath '//subscriber[@msisdn="447700900142"]/plan/text()' subscribers.xmlxmllint is part of libxml2, and the expression syntax is XPath. That last one is why people who work with XML daily stop opening it in editors at all. Being able to ask a precise question of a 40MB file, without loading it, is the thing XML tooling genuinely does better than the JSON equivalent.
Making dense XML readable
Machine-generated XML usually arrives with no line breaks at all:
<subscribers><subscriber msisdn="447700900142"><plan>Unlimited 5G</plan><roaming>true</roaming><rsrp unit="dBm">-92</rsrp></subscriber></subscribers>The same document, formatted:
<subscribers>
<subscriber msisdn="447700900142">
<plan>Unlimited 5G</plan>
<roaming>true</roaming>
<rsrp unit="dBm">-92</rsrp>
</subscriber>
</subscribers>When it will not open
If a program rejects the file, it is nearly always one of these:
- An unescaped
&. By far the most common. A bare ampersand in text must be&. Company names beat this out of people eventually. - A stray
<or>in text content, which must be</>. - Mismatched or unclosed tags — every
<tag>needs its</tag>, correctly nested. - More than one root element. An XML document gets exactly one outermost tag.
- Smart quotes around attribute values, courtesy of Word.
- A BOM before the
<?xmldeclaration. Nothing may precede it, not even an invisible byte.
If you want to understand what you are looking at rather than just open it, this guide to XML files covers attributes, namespaces and schemas. If you were given XML but would rather work in JSON, the comparison here explains what changes between them.
The short version
- Browser for a quick read — it parses properly and reports errors with a line number.
- VS Code if you handle XML regularly.
xmllint --nooutto check validity,--formatto make it readable.- Never save from Word or Excel. Smart quotes and restructuring will break the file.
- If it will not parse, look for a bare
&first.