Monday, January 17, 2005

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

Monday, January 17, 2005 4:53:26 AM (Eastern Standard Time, UTC-05:00)
What else have you been upto :) ?
Friday, February 04, 2005 5:09:23 AM (Eastern Standard Time, UTC-05:00)
There's one on gotdotnet called ppxml
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=C9B8840C-FE2F-404A-B016-D7026AA0F611
Vinil
Tuesday, May 06, 2008 6:22:23 AM (Eastern Standard Time, UTC-05:00)
Thanks. This helped.
PkS
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview