Click here to Skip to main content
15,918,243 members
Articles / Programming Languages / XML

Formatting XML - A Code snippet

Rate me:
Please Sign up or sign in to vote.
3.82/5 (29 votes)
14 Jan 2007 83.1K   19   13
How to quickly format XML for a nicer display.


I know this is basic stuff for many here, but as I couldn't find any similar article on this site I thought Id share this useful code snippet for those not so au-fait with the Xml classes.

Basically, I wanted a quick and easy way to get from a non-formatted string of XML, to something that could be nicely displayed in a textbox (ie with indenting and line breaks) without too much mucking about.

Using the code

Simple. Just call the method and display the result, eg. in a Windows forms textbox. Displaying in Webform elements may need the newline characters changed which can be done easily using String.Replace.

The code:

using System.Text;
using System.Xml;

. . .

/// <summary>
/// Returns formatted xml string (indent and newlines) from unformatted XML
/// string for display in eg textboxes.
/// </summary>
/// <param name="sUnformattedXml">Unformatted xml string.</param>
/// <returns>Formatted xml string and any exceptions that occur.</returns>

private string FormatXml(string sUnformattedXml)
    //load unformatted xml into a dom
    XmlDocument xd = new XmlDocument();

    //will hold formatted xml
    StringBuilder sb = new StringBuilder();

    //pumps the formatted xml into the StringBuilder above
    StringWriter sw = new StringWriter(sb);

    //does the formatting
    XmlTextWriter xtw = null;

        //point the xtw at the StringWriter
        xtw = new XmlTextWriter(sw);

        //we want the output formatted
        xtw.Formatting = Formatting.Indented;

        //get the dom to dump its contents into the xtw 
        //clean up even if error
        if (xtw != null)

    //return the formatted xml
    return sb.ToString();

Happy coding!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 5 Pin
Piyush Patell25-Jun-13 23:52
Piyush Patell25-Jun-13 23:52 
GeneralMy vote of 5 Pin
ikghosh20-Mar-13 18:32
professionalikghosh20-Mar-13 18:32 
GeneralMy vote of 5 Pin
Duc Huy Nguyen25-May-12 9:29
Duc Huy Nguyen25-May-12 9:29 
QuestionVB.Net version Pin
Dennis O.11-Jul-11 4:31
professionalDennis O.11-Jul-11 4:31 
GeneralMy vote of 5 Pin
ernie_hudds24-May-11 3:00
ernie_hudds24-May-11 3:00 
Simple and easy to understand.
GeneralMy vote of 5 Pin
freedeveloper22-Dec-10 11:06
professionalfreedeveloper22-Dec-10 11:06 
GeneralThanks for Sharing this Pin
jgdean1429-Sep-09 12:27
jgdean1429-Sep-09 12:27 
GeneralU made my day Pin
perren25-Mar-09 12:15
perren25-Mar-09 12:15 
GeneralThanks Pin
sumit703412-Feb-09 18:51
sumit703412-Feb-09 18:51 
GeneralMy vote of 1 Pin
Kevin Dance4-Dec-08 2:50
Kevin Dance4-Dec-08 2:50 
GeneralThx Pin
binabic17-Apr-08 20:55
binabic17-Apr-08 20:55 
GeneralQl snippet Pin
lepipele30-Oct-07 15:59
lepipele30-Oct-07 15:59 
GeneralNice! Pin
craigg7516-Jan-07 4:23
craigg7516-Jan-07 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.