Format XML in C#
This is the code for the options you picked. It produces the same text as the page:
XDocument.ToString() is the short version: it writes the same two-space layout and leaves out the declaration. ToString(SaveOptions.DisableFormatting) gives the minified form.
What XmlWriter changes, and this page copies
- Whitespace between elements is dropped.
XDocument.Parseignores it by default, so old indentation does not survive. Load withLoadOptions.PreserveWhitespaceto keep it, but thenXmlWriterkeeps your original layout instead of indenting. - Mixed content stays on one line. An element with both text and child elements, such as
<p>Hello <b>you</b></p>, is not indented, because new line breaks would change its text. The same goes for everything inside it. - Empty elements are written as
<item />, with a space before the slash. - Attribute values are written in double quotes, and line breaks and tabs inside them become

,
and	so they survive the next parse. - Entities such as
éare replaced by the character; only<,&and>are written back as entities, plus"inside attribute values. xml:space="preserve"keeps whitespace-only text inside that element.- A DOCTYPE is written back with its internal subset in brackets. Entities it declares are expanded. DOCTYPEs with parameter entities or external references are reported instead of formatted.
The utf-16 declaration trap
Save an XDocument through a StringWriter or StringBuilder and the declaration says encoding="utf-16", even when the input said UTF-8. A .NET string is UTF-16, and XmlWriter reports the encoding of what it writes to. If that text then goes into a UTF-8 file or HTTP response, some parsers refuse it. Pick encoding="utf-16" above to see it. The fixes:
- Write to a
MemoryStreamwithEncoding = new UTF8Encoding(false)(the snippet does this when you pick utf-8). Without it,XmlWriterstarts the stream with a UTF-8 byte order mark. - Or subclass
StringWriterand overrideEncodingto returnEncoding.UTF8. - Or leave the declaration out (
OmitXmlDeclaration = true); UTF-8 is the default for XML without one.
Error messages
If the XML is not well-formed, the page shows the message XmlException would give, for example The 'item' start tag on line 3 position 4 does not match the end tag of 'items'. Line 4, position 3. (load the "Broken XML" example). Click the message to jump to that place in the input. To check XML against a schema (XSD), use the XML validator.
FAQ
How do I pretty print XML in C#?
XDocument.Parse(xml).ToString() indents with two spaces. For other settings, save through XmlWriter.Create(output, new XmlWriterSettings { Indent = true, IndentChars = " " }), as in the snippet above.
Why are my line endings \r\n?
XmlWriterSettings.NewLineChars defaults to Environment.NewLine, which is \r\n on Windows. Set it to "\n" for the same output on every OS. This page uses \n.
Does the formatter change my data?
No, only whitespace between elements, quote style and escaping. Text, attribute values, CDATA sections, comments and processing instructions keep their content.
How do I format XML in C#?
Load it with XDocument.Parse and save it through an XmlWriter created with XmlWriterSettings { Indent = true }. XDocument.ToString() gives the same two-space layout without the XML declaration.
Why does my XML declaration say utf-16?
A StringWriter or StringBuilder holds UTF-16 text, so XmlWriter writes encoding="utf-16" into the declaration. Write to a MemoryStream with a UTF8Encoding, or use a StringWriter subclass that reports UTF-8, when the text is meant to be saved as UTF-8.
Why is some text not indented?
An element that holds both text and child elements (mixed content) is written on one line, because new indentation would change its text. XmlWriter does the same, and this page copies it.
Is my XML uploaded?
No. The formatter runs in your browser.