About XML files
RFC 7303 registers both application/xml and text/xml; they are now treated the same, and application/xml is the usual choice. The encoding comes from the charset parameter if present, otherwise from the <?xml encoding="…"?> declaration, otherwise UTF-8. Formats built on XML get their own +xml types, like image/svg+xml and application/rss+xml.
Other types seen for these files:
text/xml
. Accept them when reading uploads, but send application/xml yourself.
File signature
XML files start with 3C 3F 78 6D 6C ("<?xml"), when the declaration is present. The extension and the type a browser reports for an upload both
come from the file name, so check these bytes on the server before trusting a file.
Serve .xml with the right Content-Type
Content-Type: application/xml; charset=utf-8
application/xml xml xsd xsl;
AddType application/xml .xml .xsd .xsl
AddCharset UTF-8 .xml .xsd .xsl
@xml path *.xml *.xsd *.xsl
header @xml Content-Type "application/xml; charset=utf-8"
curl -sI https://example.com/file.xml | grep -i '^content-type'
# Content-Type: application/xml; charset=utf-8
Set the type in code
res.set('Content-Type', 'application/xml; charset=utf-8');
import mimetypes
mimetypes.add_type('application/xml', '.xml') # if your system doesn't know it
mimetypes.guess_type('file.xml')[0] # 'application/xml'
mime.AddExtensionType(".xml", "application/xml; charset=utf-8")
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
Accept .xml uploads
<input type="file" accept=".xml,.xsd,.xsl,application/xml">
The accept attribute only filters the file picker; users can still choose another file. Listing the
extension as well as the type matters, because operating systems don’t always report the type you expect.
Related types
More data and text
.txt , .csv , .tsv , .md , .ndjson , .jsonld , .yaml , .geojson , .ics , .sql . Look up any extension or type with the MIME type lookup.
Sources
Spotted a mistake? Email hello@devtoolsmith.com and we’ll fix it.
Frequently asked questions
What is the MIME type for .xml files?
application/xml. You may also see text/xml, which is older or unofficial; send application/xml. It is a text format, so add the character set: "Content-Type: application/xml; charset=utf-8".
Why does my server send the wrong Content-Type for .xml?
Web servers pick the type from the file extension using a table (mime.types in nginx and Apache). If .xml is missing from that table, they send a default such as application/octet-stream or text/plain. Add the mapping as shown on this page, then check the response with "curl -sI".
How can I check that a file really is XML?
Look at the first bytes rather than the extension or the type the browser reports, which both come from the file name. XML files start with 3C 3F 78 6D 6C ("<?xml"), when the declaration is present. The MIME type lookup on this site reads them for you when you drop a file on it.