This code takes in an XML file and formats it to have tabs and return characters.
Code:
using System;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Diagnostics;
//using System.Runtime.InteropServices;
namespace Clean_XML
{
public partial class Form1 : Form
{
private System.Text.StringBuilder mclsStr;
/*
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency);
*/
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//long iStart,freq;
//QueryPerformanceFrequency(out freq);
//QueryPerformanceCounter(out iStart);
XmlDocument ms = new XmlDocument();
string sInput, sOutput;
string mOut;
sInput = txtInput.Text;
sOutput = txtOutput.Text;
if (File.Exists(sInput) == false)
{
MessageBox.Show("Please enter or drag a valid file into the input text file");
return;
}
if (sOutput.Length == 0)
{
sOutput = sInput + ".out";
txtOutput.Text = sOutput;
}
mclsStr = new System.Text.StringBuilder();
try
{
ms.Load(sInput);
}
catch(Exception exc)
{
MessageBox.Show("An error occured parsing the file" + (char)10 + exc.ToString());
return;
}
GetNodesRec((ms.ChildNodes), 0);
mOut = mclsStr.ToString();
Encoding enCode = Encoding.ASCII;
if (ms.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
{
XmlDeclaration decl = (XmlDeclaration)ms.FirstChild;
if (decl.Encoding == "UTF-7")
enCode = Encoding.UTF7;
else if (decl.Encoding == "UTF-8")
enCode = Encoding.UTF8;
else if (decl.Encoding == "UTF-32")
enCode = Encoding.UTF32;
else //if (decl.Encoding == "UTF")
enCode = Encoding.Unicode;
}
StreamWriter sw = new StreamWriter(sOutput, false, enCode);
sw.Write(mclsStr.ToString());
sw.Close();
//long iEnd;
//QueryPerformanceCounter(out iEnd);
//double iTime = (double)(iEnd - iStart) / (double)freq;
//Form1.ActiveForm.Text = "Processing Took: " + Math.Round(iTime,2).ToString() + " Seconds";
DialogResult drAns = MessageBox.Show("Done - " + sOutput + " has been created. Do you want to open this file", "Open File", MessageBoxButtons.YesNo);
if (drAns == DialogResult.Yes)
{
Process p = new Process();
p.StartInfo.FileName = @"notepad";
p.StartInfo.Arguments = sOutput;
p.StartInfo.CreateNoWindow = true;
p.Start();
}
}
private void GetNodesRec(System.Xml.XmlNodeList oNodes, int iTab)
{
for (int iLoop = 0; iLoop < oNodes.Count; iLoop++)
{
if (oNodes[iLoop].ChildNodes.Count > 0)
{
mclsStr.AppendLine(GetTabs(iTab) + FirstPart(oNodes[iLoop].OuterXml));
GetNodesRec((oNodes[iLoop].ChildNodes), iTab + 1);
mclsStr.AppendLine(GetTabs(iTab) + LastPart((oNodes[iLoop].OuterXml)));
}
else
{
mclsStr.AppendLine(GetTabs(iTab) + oNodes[iLoop].OuterXml);
}
}
}
private string LastPart(string p)
{
int iPos;
iPos = p.LastIndexOf("<");
return p.Substring(iPos);
}
private string FirstPart(string p)
{
int iPos;
iPos = p.IndexOf(">")+1;
return p.Substring(0, iPos);
}
private string GetTabs(int iTab)
{
if (iTab < 0)
iTab = 1;
return new string((char)9, iTab);
}
private void OnDragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
txtInput.Text = files[0];
SetOutPutFileName();
}
private void OnDragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
e.Effect = DragDropEffects.All;
}
private void SetOutPutFileName()
{
if (txtOutput.Text.Length == 0)
txtOutput.Text = txtInput.Text + ".out";
}
}
}
Designer Code:
Code:
namespace Clean_XML
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.txtInput = new System.Windows.Forms.TextBox();
this.txtOutput = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(205, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Go";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txtInput
//
this.txtInput.AllowDrop = true;
this.txtInput.Location = new System.Drawing.Point(79, 12);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(201, 20);
this.txtInput.TabIndex = 1;
this.txtInput.DragDrop += new System.Windows.Forms.DragEventHandler(this.OnDragDrop);
this.txtInput.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
//
// txtOutput
//
this.txtOutput.Location = new System.Drawing.Point(79, 38);
this.txtOutput.Name = "txtOutput";
this.txtOutput.Size = new System.Drawing.Size(201, 20);
this.txtOutput.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(50, 13);
this.label1.TabIndex = 3;
this.label1.Text = "Input File";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 38);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(58, 13);
this.label2.TabIndex = 4;
this.label2.Text = "Output File";
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(286, 92);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtOutput);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "XML Cleaner";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.TextBox txtOutput;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}