Ever had badly formatted XML that you wanted to have looking clean? Here is a little bit of C# that cleans up the XML indentation and makes a pretty printed XML for you.
using System;
using System.IO;
using System.Xml;
class CMain
{
static void Main(string[] files)
if (files.Length == 0)
Console.WriteLine("Syntax:\n\txmlpp <filename> [<outfilename>]");
XmlDocument doc = new XmlDocument();
XmlTextReader rd = new XmlTextReader(files[0]);
rd.WhitespaceHandling = WhitespaceHandling.None;
doc.Load(rd);
rd.Close();
XmlTextWriter wr = new XmlTextWriter(files.Length == 1 ? files[0] : files[1], null);
wr.Formatting = Formatting.Indented;
doc.Save(wr);
wr.Close();
}
I wrote this down because I was doing some XML manipulation with the REXML library for Ruby yesterday and REXML seemed to dump relatively ugly XMLs no matter what formatting options I gave it.
Usage Examples:
xmlpp file.xml
xmlpp file.xml outfile.xml
Download src and binary
Remember Me
a@href@title, strike
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Roshan James
E-mail