Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

PrettyXML (.NET Port)

4.10/5 (3 votes)
12 Jul 2012CPOL 14.7K  
This is a .NET port of a VBScript utility

Introduction

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:

C++
<?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.

Background

All credit goes to the original author: Robert McMurray.

The original blog: http://blogs.msdn.com/b/robert_mcmurray/archive/2012/07/06/creating-quot-pretty-quot-xml-using-xsl-and-vbscript.aspx

The code

C#
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");
                return;
            }
 
            string inFile = args[0];
            string outFile = args[1];
            if (File.Exists(inFile))
            {
                string strStylesheet = "<xsl:stylesheet version=\"1.0\" xmlns:" + 
                   "xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output " + 
                   "method=\"xml\" indent=\"yes\"/><xsl:template " + 
                   "match=\"/\"><xsl:copy-of select=\".\"/></xsl" + 
                   ":template></xsl:stylesheet>";
                // 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);
            }
            else
            {
                Console.WriteLine("{0} cannot be found", inFile);
                return;
            }
        }
    }
}

Using the code

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

C++
prettyxml <source.xml> <destination.xml>

If we run the utility on the XML above:

prettyxml ednrgtest.xml ednrgformatted.xml

The result is:

XML
<?xml version="1.0" encoding="utf-8"?>
<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>

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.

License

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