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";
}
}
}
Saturday, May 30, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment