VS Code formats XML with Shift+Alt+F on Windows, Ctrl+Shift+I on Linux, or Shift+Option+F on macOS, as well as through the “Format Document” command; if no formatter is registered, use a browser-based formatter and paste the result back into the file.
The important distinction is that “Format Document” is a VS Code command, while the actual formatting is performed by a registered language formatter. An XML file can open with syntax support but still produce a “No document formatter found” message when no XML formatter is available. The browser workflow below is a fallback for that situation, not an automatic way to edit the open file.

Format XML inside VS Code
Start with VS Code’s built-in commands because they apply the formatter directly to the editor buffer. The result is easy to inspect with the editor’s diff view before you save it.
- Open the XML file. Confirm that VS Code identifies the language as XML. If the status bar shows a different language, open the language-mode picker and choose XML.
- Save or checkpoint the current version. If the file is under version control, review its existing state before formatting so a later diff clearly shows only the formatting changes.
- Format the whole document. On Windows, press Shift+Alt+F. On Linux, press Ctrl+Shift+I; on macOS, press Shift+Option+F. If a shortcut does nothing, use the Command Palette instead.
- Open the Command Palette. Press Ctrl+Shift+P on Windows or Linux, or Command+Shift+P on macOS. Type Format Document and select the matching command.
- Format only a selection when needed. Highlight the XML fragment and run Format Selection from the Command Palette. This is useful for a copied payload inside an otherwise unrelated document.
- Review and save. Check that the hierarchy is clearer, that attributes and text remain correct, and that no unexpected content was removed. Save the file with Ctrl+S or Command+S.
| VS Code command | Scope | Best use |
|---|---|---|
| Format Document | The active editor document | Formatting a complete XML file |
| Format Selection | The selected XML fragment | Cleaning up a small payload or embedded block |
| Format Document With... | A chosen formatter provider | Selecting between multiple installed XML formatters |
If the command reports that no document formatter is registered, repeating the shortcut will not add one. You need either a VS Code formatter extension or the separate browser workflow.
Resolve a missing XML formatter
VS Code’s built-in XML language support does not guarantee a document-formatting provider for every XML file. An extension can add that capability, but the related browser product discussed here is not a VS Code extension and will not appear in the Extensions view.
- Open the Extensions view. Press Ctrl+Shift+X on Windows or Linux, or Command+Shift+X on macOS.
- Search for an XML formatter. Review the extension’s publisher, documentation, and stated compatibility. Choose one that explicitly supports XML formatting in Visual Studio Code rather than assuming every XML-related extension registers a formatter.
- Install the extension and reload VS Code if prompted. Return to the XML file after the extension activates.
- Run “Format Document” again. If more than one provider is available, use Format Document With... to choose the appropriate formatter, then inspect the diff and save.
If installing extensions is unsuitable for your environment, use an editor-independent formatter instead. This keeps the XML task in the browser and avoids changing the project’s extension configuration.
Format XML in a browser and copy it back
The XML Formatter provides a browser-based option when you do not want to install a VS Code extension. It formats or minifies pasted XML locally, but it does not connect to VS Code or modify a file on your disk.
- Copy the XML from VS Code. Select the complete document or the portion you want to process and copy it to the clipboard.
- Open XML Formatter. Paste the copied XML into the input box, or type it there directly.
- Choose an indentation style. Select 2 spaces, 4 spaces, or Tab according to the convention used by the project.
- Process the document. Click Format to pretty-print the XML, or click Minify to compress it onto one line.
- Review the result. If the XML is malformed, read the reported error, return to the source, correct the problem, and run the formatter again.
- Copy the processed output. Click Copy to place the formatted or minified XML on the clipboard.
- Return to VS Code and replace the relevant content. Paste the output over the selected XML or the full buffer, review the changed indentation or line breaks, and save the file.
This workflow is intentionally manual: the browser tool produces text for you to paste into VS Code. It does not watch the open file, rewrite it automatically, or preserve the file’s location and name.
Choose pretty-printing or minification
Formatting and minification solve different problems. Choose the output based on whether you need a readable source file or a compact representation.
| Goal | Control | Output | Best fit |
|---|---|---|---|
| Readable XML structure | Format | Pretty-printed with 2-space, 4-space, or tab indentation | Source files, configuration, and review |
| Compact XML | Minify | Whitespace between tags removed and content placed on one line | Payload transmission or embedding |
Minify removes layout whitespace between tags while keeping meaningful text exactly as-is. That makes it useful when reducing payload size, but the result should still be reviewed because a compact file can make manual inspection harder.
Know which XML details are preserved
XML contains constructs that a simple text replacement tool can damage. The browser formatter handles these structures explicitly, but its layout rules still matter when you decide whether the result is suitable for your project.
- Attribute values: A greater-than character inside an attribute value is parsed correctly instead of prematurely ending the tag.
- Self-closing elements: Elements written in self-closing form are preserved exactly as written.
- Comments: Comments are retained and indented along with the rest of the document during formatting.
- CDATA sections: CDATA content passes through untouched, including reserved symbols and text that resembles markup.
- Document declarations: During formatting, the XML declaration and processing instructions remain on their own line at the top. DOCTYPE declarations are preserved verbatim.
- Mixed content: Text mixed with inline child elements, as found in some SVG, XHTML, and documentation content, stays together on one line exactly as written. Formatting only re-indents the space between tags.
- Plain-text elements: The formatter normalizes leading and trailing whitespace when an element contains plain text only.
- Space preservation: The xml:space="preserve" attribute does not receive special treatment, so do not assume that every formatting-related whitespace byte will remain identical.
Consequently, the result is not guaranteed to be byte-for-byte identical to the source. Minify is the relevant choice when meaningful text must remain exactly as-is, because it changes layout whitespace rather than rewriting meaningful text; it still removes whitespace between tags. For production files, review a diff instead of assuming the transformation changed only indentation.
Correct malformed XML before saving
The formatter parses the document while processing it and reports common structural problems instead of silently returning broken output. Typical errors include:
- Elements with unclosed tags.
- Mismatched or stray closing tags.
- Comments that end without a closing marker.
- CDATA sections or processing instructions that are not terminated properly.
Read the error message and use its line and column location when one is provided. Correct the source in VS Code, then format it again. Do not remove characters merely to silence the parser when doing so would change the XML’s intended structure.
These checks detect syntax and structural faults; they are not a complete compliance check. The formatter does not validate against an XSD, verify data types, or enforce namespace rules. If your project requires those checks, perform that validation separately before deployment.
Review the final VS Code change
- Compare the formatted file with the original. Look for unexpected line breaks, changed indentation, or removed whitespace around text.
- Check sensitive XML constructs. Review comments, CDATA, processing instructions, DOCTYPE content, self-closing elements, and attribute values containing special characters.
- Confirm that meaningful content is unchanged. Pay particular attention to mixed content and plain-text elements whose surrounding whitespace may be normalized.
- Save only after the review. For code under version control, keep the diff available for the next reviewer and avoid committing accidental formatting churn.
The browser formatter runs entirely in your browser using local JavaScript processing. Your XML is not uploaded to a server, logged, or stored, and the tool can continue working without a network connection. It requires no account or installation, although very large documents are still limited by what your own device can handle.
If you're weighing options, Convert XML to CSV in Java: A Repeatable Record Workflow covers this in detail.
If you're weighing options, XML JSON Converter: Mapping Rules and Workflow covers this in detail.