Xml normalization utility
August 26, 2010 at 1:09 PM
—
alex
Periodically, I need to parse a string, which represents an xml file into formatted file with intendation. Here is a simple .Net code which does this and can be compiled into a console application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace NormXml
{
class Program
{
private static string inputFileName;
private static string outputFileName;
private static void Main(string[] args)
{
ParseCommandLine();
if (CommandLineIncomplete())
{
PrintHelp();
return;
}
var doc = new XmlDocument();
doc.Load(inputFileName);
doc.Save(outputFileName);
}
private static void PrintHelp()
{
Console.WriteLine("Xml normalizer utility");
Console.WriteLine("normxml.exe input.txt output.txt");
}
private static void ParseCommandLine()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length >= 2)
{
inputFileName = args[ 1 ];
}
if (args.Length >= 3)
{
outputFileName = args[ 2 ];
}
}
private static bool CommandLineIncomplete()
{
return ( String.IsNullOrEmpty( inputFileName ) || String.IsNullOrEmpty( outputFileName ) );
}
}
}