Click here to Skip to main content
6,597,576 members and growing! (18,295 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Formatting XML - A Code snippet

By Ian Randell

How to quickly format XML for a nicer display.
C#, XML, Windows, .NET, Visual Studio, Dev
Posted:14 Jan 2007
Views:16,708
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 4.33 Rating: 3.33 out of 5
6 votes, 30.0%
1

2

3
5 votes, 25.0%
4
9 votes, 45.0%
5

Introduction

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();
    xd.LoadXml(sUnformattedXml);

    //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;

    try
    {
        //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 

        xd.WriteTo(xtw);
    }
    finally
    {
        //clean up even if error

        if (xtw != null)
            xtw.Close();
    }

    //return the formatted xml

    return sb.ToString();
}

Happy coding!

License

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


Member

Location: United Kingdom United Kingdom

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralThanks for Sharing this Pinmemberjgdean1413:27 29 Sep '09  
GeneralU made my day Pinmemberperren13:15 25 Mar '09  
GeneralThanks Pinmembersumit703419:51 12 Feb '09  
GeneralMy vote of 1 PinmemberKevin Dance3:50 4 Dec '08  
GeneralThx Pinmemberbinabic21:55 17 Apr '08  
GeneralQl snippet PinmemberPredrag Tomasevic16:59 30 Oct '07  
GeneralNice! Pinmembercraigg755:23 16 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jan 2007
Editor: Chris Maunder
Copyright 2007 by Ian Randell
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project