How to encode XML to Base64
- Paste the XML document into Input · text, or press Sample to load a small one.
- Set Charset to the encoding named in the XML declaration. UTF-8 is preselected and right for most documents.
- Copy the result from Output · Base64. Leave Line wrap on None for JSON and HTTP fields; some XML-based protocols accept 76-character lines.
- Download saves the Base64 as
base64.txt.
The sample, an XML declaration followed by a <note> element, encodes to a string starting with PD94bWwg, which is the Base64 of <?xml . Nearly every XML document with a declaration starts this way, which makes encoded XML easy to recognize. Base64 to XML decodes it again.
Match the charset to the XML declaration
An XML parser decides how to read the bytes from the encoding attribute in the declaration (or from a byte order mark). If the bytes and the declaration disagree, the receiving side reads different characters than you wrote.
| Document | Charset used here | Base64 | The parser reads |
|---|---|---|---|
<?xml version="1.0" encoding="ISO-8859-1"?><n>café</n> | ISO-8859-1 | PD94bWwg...PjxuPmNhZuk8L24+ | café |
| The same document | UTF-8 | PD94bWwg...PjxuPmNhZsOpPC9uPg== | café |
The second row is a classic integration bug: the text was saved as UTF-8 but still declares Latin-1. Either choose the declared charset here, or, better, change the declaration to UTF-8 and keep UTF-8 everywhere.
Where XML travels as Base64
- SOAP and REST APIs that accept a whole XML document inside a JSON or XML field, so its angle brackets and quotes need no escaping. E-invoicing and EDI services often take invoices (for example UBL documents) this way.
- SAML single sign-on. A SAML response is an XML document, Base64-encoded and posted by the browser. The SAML decoder turns
SAMLResponseandSAMLRequestvalues back into XML. - Configuration and secrets. XML config files stored in environment variables or Kubernetes Secrets are Base64 so they fit on one line.
When a document is digitally signed (XML-DSig, used in SAML and many e-invoicing schemes), encode the exact bytes you received or produced. Re-indenting, changing line endings or re-saving the file in an editor can invalidate the signature even though the XML means the same thing.
Size and line wrapping
Base64 adds about 33% to the document. XML is verbose to begin with, so large documents are often gzipped before encoding; the gzip and Base64 tool does both steps. Line wrap at 76 characters is accepted by MIME-based transports and by most XML Schema validators for xs:base64Binary content. JSON-based APIs usually expect one unbroken line.
XML to Base64 in code
Read the file as bytes and encode those, so the declaration and the bytes cannot drift apart:
import base64
with open("invoice.xml", "rb") as f:
b64 = base64.b64encode(f.read()).decode("ascii")
// Node.js
const fs = require("fs");
const b64 = fs.readFileSync("invoice.xml").toString("base64");
On Linux, base64 -w 0 invoice.xml prints the same single line. See Base64 in Python and Base64 in Node.js for streaming large files.
FAQ
How do I convert an XML file to Base64?
Paste the XML above and copy the result. For files, encode the raw bytes instead of re-typing the text: base64.b64encode(open("doc.xml", "rb").read()) in Python, or base64 -w 0 doc.xml on Linux.
Which charset should I choose for XML?
The one in the XML declaration. <?xml version="1.0" encoding="UTF-8"?> or no encoding attribute means UTF-8, the default. If it says ISO-8859-1 or Windows-1252, pick that, or change the declaration to UTF-8.
Can I reformat the XML before encoding it?
Only if nothing depends on the exact bytes. Indentation changes the Base64, and for signed XML (SAML assertions, signed invoices) it can break the signature.
Where does xs:base64Binary fit in?
That XML Schema type is the reverse case: Base64 inside an XML element, used to embed files in SOAP messages and invoices. Encode the file with File to Base64 and paste the result into the element.