Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C# 4.0
Tip/Trick

PrettyXML (.NET Port)

Rate me:
Please Sign up or sign in to vote.
4.10/5 (3 votes)
12 Jul 2012CPOL 14.3K   1   3
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)


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

Comments and Discussions

 
SuggestionAnother way Pin
Daniele Fusi14-Jul-12 0:00
Daniele Fusi14-Jul-12 0:00 
Interesting approach, anyway I often find easier to just load the XML with XDocument and use XDocument Save method, which by default applies formatting to the output.
GeneralMy vote of 4 Pin
Guillaume Leparmentier12-Jul-12 8:22
Guillaume Leparmentier12-Jul-12 8:22 
GeneralRe: My vote of 4 Pin
ednrg13-Jul-12 3:46
ednrg13-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.