Click here to Skip to main content
12,509,464 members (63,392 online)
Click here to Skip to main content
Add your own
alternative version

Stats

7.6K views
1 bookmarked
Posted

PrettyXML (.NET Port)

, 12 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

<?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

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:

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"?>
<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)

Share

About the Author

ednrg
Architect
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 12 Jul 2012
Article Copyright 2012 by ednrg
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid