Click here to Skip to main content
12,560,241 members (57,739 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked

Formatting XML - A Code snippet

, 14 Jan 2007
Rate this:
Please Sign up or sign in to vote.
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;

. . .

/// <span class="code-SummaryComment"><summary>

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


About the Author

Ian Randell
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Piyush Patell25-Jun-13 23:52
memberPiyush Patell25-Jun-13 23:52 
GeneralMy vote of 5 Pin
KoushikAmit20-Mar-13 18:32
memberKoushikAmit20-Mar-13 18:32 
GeneralMy vote of 5 Pin
Duc Huy Nguyen25-May-12 9:29
memberDuc Huy Nguyen25-May-12 9:29 
QuestionVB.Net version Pin
FidoDildo11-Jul-11 4:31
memberFidoDildo11-Jul-11 4:31 
GeneralMy vote of 5 Pin
earnshaw194324-May-11 3:00
memberearnshaw194324-May-11 3:00 
GeneralMy vote of 5 Pin
freedeveloper22-Dec-10 11:06
memberfreedeveloper22-Dec-10 11:06 
GeneralThanks for Sharing this Pin
jgdean1429-Sep-09 12:27
memberjgdean1429-Sep-09 12:27 
GeneralU made my day Pin
perren25-Mar-09 12:15
memberperren25-Mar-09 12:15 
GeneralThanks Pin
sumit703412-Feb-09 18:51
membersumit703412-Feb-09 18:51 
GeneralMy vote of 1 Pin
Kevin Dance4-Dec-08 2:50
memberKevin Dance4-Dec-08 2:50 
GeneralThx Pin
binabic17-Apr-08 20:55
memberbinabic17-Apr-08 20:55 
Useful for me, thank you for your work and sharing Smile | :)
GeneralQl snippet Pin
Predrag Tomasevic30-Oct-07 15:59
memberPredrag Tomasevic30-Oct-07 15:59 
GeneralNice! Pin
craigg7516-Jan-07 4:23
membercraigg7516-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161026.1 | Last Updated 14 Jan 2007
Article Copyright 2007 by Ian Randell
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid