SourceCode/PROMS/fmtxml/PrettyPrint/frmPrettyPrint.cs

96 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace PrettyPrint
{
public partial class frmPrettyPrint : Form
{
public string MyStatus
{
get { return tsslStatus.Text; }
set { tsslStatus.Text = value; Application.DoEvents(); }
}
public frmPrettyPrint()
{
InitializeComponent();
}
private void btnPretty_Click(object sender, EventArgs e)
{
FileInfo fi = new FileInfo(tbOutputFileName.Text);
using(FileStream fs = fi.Create())
{
using (StreamWriter sw = new StreamWriter(fs))
{
XmlDocument doc = new XmlDocument();
//get your document
doc.Load(tbInputFileName.Text);
//create reader and writer
XmlNodeReader xmlReader = new XmlNodeReader(doc);
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
//set formatting options
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 1;
xmlWriter.IndentChar = '\t';
//write the document formatted
xmlWriter.WriteNode(xmlReader, true);
xmlWriter.Close();
xmlReader.Close();
sw.Close();
}
fs.Close();
}
}
private void btnFolderBrowse_Click(object sender, EventArgs e)
{
fbd.SelectedPath = tbFolder.Text;
if (fbd.ShowDialog() == DialogResult.OK)
tbFolder.Text = fbd.SelectedPath;
}
private void btnFolderPretty_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(tbFolder.Text);
FileInfo [] files = di.GetFiles("*.xml");
tspbStatus.Maximum = files.Length;
tspbStatus.Visible = true;
tspbStatus.Value = 0;
foreach(FileInfo fi in files)
{
tspbStatus.Value ++;
MyStatus = string.Format("Processing {0}", fi.Name);
FixXML(fi);
}
tspbStatus.Visible = false;
MyStatus = "Ready";
}
private void FixXML(FileInfo fi)
{
DateTime lastModified = fi.LastWriteTime;
XmlDocument doc = new XmlDocument();
//get your document
doc.Load(fi.FullName);
using (FileStream fs = fi.Create())
{
//create reader and writer
XmlNodeReader xmlReader = new XmlNodeReader(doc);
XmlTextWriter xmlWriter = new XmlTextWriter(fs, Encoding.Unicode);
//set formatting options
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 1;
xmlWriter.IndentChar = '\t';
//write the document formatted
xmlWriter.WriteNode(xmlReader, true);
xmlWriter.Close();
xmlReader.Close();
fs.Close();
}
fi.LastWriteTime = lastModified;
}
}
}