XML Indent Beautifuller
Console Application gets .xml file and beautiful (reformat) it by add auto indents.
Application overwrites .xml file
using System; using System.Text; using System.IO; using System.Xml; /* * Application indents XML file */ namespace XMLBeautifuller { class Program { static void Main(string[] args) { for (int i = 0; i < args.Length; i++) { if (File.Exists(args[i])) { try { FileStream file = new FileStream(args[i], FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.Unicode); XmlDocument doc = new XmlDocument(); doc.LoadXml(sr.ReadToEnd()); sr.Close(); file.Close(); xtw.Formatting = Formatting.Indented; xtw.Indentation = 1; xtw.IndentChar = '\t'; doc.WriteContentTo(xtw); xtw.Flush(); ms.Seek(0, SeekOrigin.Begin); StreamReader xmlsr = new StreamReader(ms); file = new FileStream(args[i], FileMode.Open, FileAccess.Write); StreamWriter sw = new StreamWriter(file); sw.Write(xmlsr.ReadToEnd());
sw.Close(); file.Close(); } catch (Exception e) { Console.WriteLine(e.Message + "\n" + e.Source + "\n" + e.StackTrace + "\n" + e.Data); // Console.ReadKey(); } } else { Console.WriteLine("File " + args[i] + " not found!"); // Console.ReadKey(); } } } } }
Example usage: XMLBeautifuller myfile.xml
2012.11.22 22:29:35.