Click here to Skip to main content
15,348,829 members
Articles / Programming Languages / C# 4.0
Posted 12 Jul 2012


1 bookmarked

PrettyXML (.NET Port)

Rate me:
Please Sign up or sign in to vote.
4.10/5 (3 votes)
12 Jul 2012CPOL
This is a .NET port of a VBScript utility


I read a blog yesterday about a utility to "pretty up" messy XML, and decided to port it to a .NET console app. Of course there are plenty of apps out there that do exactly what this program does, but I did it as an exercise. 

Many times you receive an XML file from another department. You open it, to find that there is no formatting, just XML without spaces, etc.

A short example:

<?xml version="1.0"?><messages><note ID="1"><to>ednrg</to><from>Bob</from><heading>Reminder</heading><body>Bring new 
documentation to the meeting at 3pm</body></note><note ID="2"><to>ednrg</to><from>Sandra</from><heading>Project 
Delivery</heading><body>Can you give me an update on whether we are still on target for delivery?</body></note></messages>

It works perfectly with any program, but it's not formatted to make it easy to read.


All credit goes to the original author: Robert McMurray.

The original blog:

The code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Xsl;
using System.Xml;
namespace PrettyXML
    class Program
        static void Main(string[] args)
            if( args.Length != 2)
                Console.WriteLine("Usage: PrettyXML source.xml destination.xml");
            string inFile = args[0];
            string outFile = args[1];
            if (File.Exists(inFile))
                string strStylesheet = "<xsl:stylesheet version=\"1.0\" xmlns:" + 
                   "xsl=\"\"><xsl:output " + 
                   "method=\"xml\" indent=\"yes\"/><xsl:template " + 
                   "match=\"/\"><xsl:copy-of select=\".\"/></xsl" + 
                // Load the style sheet.
                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Load(new XmlTextReader(new StringReader(strStylesheet)));
                // Execute the transform and output the results to a file.
                xslt.Transform(inFile, outFile);
                Console.WriteLine("{0} cannot be found", inFile);

Using the code

This simple utility creates a formatted copy of your XML. The syntax for the utility is:

prettyxml <source.xml> <destination.xml>

If we run the utility on the XML above:

prettyxml ednrgtest.xml ednrgformatted.xml

The result is:

<?xml version="1.0" encoding="utf-8"?>
  <note ID="1">
    <body>Bring new documentation to the meeting at 3pm</body>
  <note ID="2">
    <heading>Project Delivery</heading>
    <body>Can you give me an update on whether we are still on target for delivery?</body>

Nothing has changed, it's now just easier to read. It's a useless utility, but may be useful once or twice in your life.

Again, all credit goes to Robert McMurray.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

Comments and Discussions

SuggestionAnother way Pin
Daniele Fusi14-Jul-12 0:00
MemberDaniele Fusi14-Jul-12 0:00 
GeneralMy vote of 4 Pin
Guillaume Leparmentier12-Jul-12 8:22
MemberGuillaume Leparmentier12-Jul-12 8:22 
GeneralRe: My vote of 4 Pin
ednrg13-Jul-12 3:46
Memberednrg13-Jul-12 3:46 

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.