Microsoft Visual Studio can format an XML document with Edit > Advanced > Format Document, then save the reformatted content back to the file.
This guide covers the full Microsoft Visual Studio IDE rather than Visual Studio Code, which uses a separate extension-based workflow. For a file in a project, open the solution and locate the XML resource in Solution Explorer. For a standalone document, use File > Open > File and select the .xml file.
Visual Studio's built-in XML editor is the quickest option for documents you are already editing in the IDE. A browser-based formatter is useful when you need a particular indentation width or compact output, or when the native result is not convenient. The XML Formatter works in a separate browser tab; it does not directly modify or save the open Visual Studio file.

Format XML in Visual Studio
Use the built-in command when the XML is already open and you want to improve its indentation and visual nesting.
- Open the XML document. Select the .xml file in Solution Explorer, or open it through File > Open > File. Make sure you are working in Visual Studio's XML editor rather than a plain-text view.
- Place the cursor in the XML document. Click an element, attribute, or text area before running the formatting command so Visual Studio knows which document to process.
- Run Format Document. Choose Edit > Advanced > Format Document. The general Visual Studio shortcut is Ctrl+K, Ctrl+D; if your keyboard mapping differs, use the menu command.
- Resolve any XML syntax error reported by the editor. If the document is malformed, correct the unclosed element, mismatched closing tag, or other problem indicated by Visual Studio, then run the formatter again.
- Review the formatted XML. Check the nesting, attributes, text content, comments, CDATA sections, and declaration. XML whitespace can be meaningful, so inspect any element that contains ordinary text rather than assuming every space was only present for layout.
- Save the document. Press Ctrl+S after confirming that the result matches the structure you intend to keep.
If the command produces no useful change, first confirm that the file is open as XML and that no syntax error remains. Then check the XML-specific formatting preferences described below. A document already close to the configured style may show little visible difference.
Configure XML formatting options
Visual Studio provides dedicated settings for controlling how XML elements, attributes, and other content are formatted. These options are separate from general code-style settings and are useful when the team wants XML files to follow the same layout across projects.
- Open Tools > Options.
- Expand Text Editor > XML > Formatting.
- Review the available controls for XML layout and select the settings that match your project conventions.
- Select Apply or OK, place the cursor in an XML document again, and run Format Document to test the configuration.
These preferences control presentation rather than schema compliance. They can make the XML editor's result more predictable, but they do not repair invalid markup or certify that the document satisfies an XSD, DTD, data type, or namespace requirement. Keep a separate validation step when those rules matter to the project.
Use a browser formatter as a separate workflow
Use the browser-based XML Formatter when you want explicit control over two-space, four-space, or tab indentation, or when you need XML on a single line before sending it over the wire or embedding it in another file. Because it is a separate application surface, the workflow requires copying the source into the tool and copying the result back into Visual Studio.
- Copy the XML from Visual Studio. Select the document text you want to process and copy it. For a large file, work from a copy first rather than replacing the original immediately.
- Paste or type the XML into the formatter. Open the XML Formatter and place the XML in the input box.
- Choose an indentation style. Select two spaces, four spaces, or Tab, depending on the layout you want for each nesting level.
- Run the appropriate operation. Select Format to pretty-print the hierarchy, or select Minify to remove layout whitespace and produce one compact line.
- Read any error and correct the source. If the XML is malformed, use the reported message to locate the problem in Visual Studio, fix it there, and run the operation again.
- Review the result before copying it. Confirm that the structure and content are suitable for your use, especially around comments, CDATA, declarations, and text-bearing elements.
- Copy and paste the output back. Select Copy in the formatter, replace the selected XML in Visual Studio with the result, and save the document.
All XML processing happens locally in the browser. The XML is not uploaded, sent to a server, logged, or stored, and the formatter can continue working offline. There is no account or installation to set up, although the amount of XML you can process still depends on the resources of your own device.
XML content the browser formatter preserves
- Comments remain in place and are indented with the surrounding document.
- CDATA sections pass through untouched, including reserved characters and markup-looking text inside the section.
- Self-closing elements are preserved exactly as written rather than expanded into a different tag form.
- XML declarations and processing instructions remain on their own line at the top, while DOCTYPE declarations are preserved verbatim.
- Attribute values containing a greater-than character are parsed correctly instead of causing the tag to end early.
There are two important whitespace boundaries. For mixed content—ordinary text mixed with inline child elements—the complete run stays on one line exactly as written so meaningful internal whitespace is not rewritten. When an element contains plain text only, however, Format can normalize its leading and trailing whitespace, and the tool does not give xml:space="preserve" special treatment. Check those cases before replacing the original file.
Minify has a narrower purpose: it removes insignificant whitespace and line breaks between tags to create one compact line while retaining meaningful text inside elements. It is therefore useful for transport or embedding, but the result should still be reviewed before it replaces a source document.
Compare the native and browser workflows
| Workflow | Output | Best fit | Important boundary |
|---|---|---|---|
| Visual Studio Format Document | Readable nesting and indentation based on the XML editor's settings | An XML file already open in a Visual Studio project | Malformed XML still needs correction; this is not a one-line minifier |
| Browser Format | Pretty-printed XML with two-space, four-space, or tab indentation | A specific indent style or a fallback outside the IDE | Plain-text-only elements may have leading or trailing whitespace normalized |
| Browser Minify | A single compact line with layout whitespace removed | Compact payloads and embedded XML | Meaningful text stays intact, but xml:space="preserve" is not specially handled |
| Browser error check | A friendly structural error message, usually with a line and column | Finding common malformed-XML problems | It is not full XSD or DTD validation |
While formatting or minifying, the browser tool parses the XML and flags problems such as unclosed tags, mismatched or stray closing tags, and unterminated comments, CDATA sections, or processing instructions. It does not check the document against an XSD, verify data types, or enforce namespace rules. Treat the result as well-formedness feedback rather than proof that the XML meets a business or schema standard.
Review the output before saving
Keep the original available while you compare the formatted version with the source. Before committing the result, confirm that the XML declaration and DOCTYPE are present, self-closing elements retain their original form, and comments and CDATA remain where you expect them. Pay particular attention to mixed text, inline SVG or XHTML content, and any element using xml:space="preserve".
For a substantial document, paste the original and formatted text into the Diff Checker to inspect the changes line by line. This makes an unexpected whitespace or content change easier to spot before you overwrite the Visual Studio file. Use the browser formatter for a controlled fallback or formatting choice, then return to the IDE, save, and verify the document in its real project context.