c#批量整頓xml格局示例。本站提示廣大學習愛好者:(c#批量整頓xml格局示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#批量整頓xml格局示例正文
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 WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count == 0)
{
MessageBox.Show("no file name ");
}
else
{
func_SearchFiles(sender, e);//獲得文件名
}
//listBox1.Items.Clear();
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
listBox1.Items.Add ( path);//顯示文件夾目次
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}
private void func_SearchFiles(object sender, EventArgs e)
{
// 獲得指定文件夾目次
string filepath = listBox1.Items[0].ToString();
DirectoryInfo baseDir = new DirectoryInfo(filepath);
// 獲得指定文件夾下的一切文件。
// 假如你須要獲得特定格局的文件,如.html 開頭的,可以寫成 baseDir.GetFiles("*.html");
FileInfo[] files = baseDir.GetFiles("*.xml");
// 界說文件名字符串
progressBar1.Visible = true;
progressBar1.Maximum = files.Length;
progressBar1.Minimum = 0;
string fileNames = string.Empty;
for (int i = 0; i < files.Length; i++)
{
// 獲得每一個文件名,並記載到 字符串 fileNames 裡
// 假如須要獲得文件的完全途徑名, files[i].FullName;
//fileNames += files[i].FullName + ",";
string xmlfile = @files[i].FullName;
MemoryStream mstream = new MemoryStream(1024);
XmlTextWriter writer = new XmlTextWriter(mstream, null);
XmlDocument xmldoc = new XmlDocument();
writer.Formatting = Formatting.Indented;
xmldoc.Load(xmlfile);
xmldoc.WriteTo(writer);
writer.Flush();
writer.Close();
Encoding encoding = Encoding.GetEncoding("utf-8");
listBox1.Items.Add("正在處置:" + @files[i].FullName);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
progressBar1.Value = i+1;
//this.ListBox1.Text += "\r\n正在處置:" + @files[i].FullName + "...\r\n";
//File myfile = new file
xmldoc.Save(@files[i].FullName);
mstream.Close();
}
// 顯示到 Label 標簽上
listBox1.Items.Add("Finish!!!!");
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
private void button2_Click(object sender, EventArgs e)
{
// this.listBox1.SelectedItem = listBox1.Items.IndexOf(0);//堅持文本顯示在最初一行
listBox1.Items.Clear();
progressBar1.Visible = false;
progressBar1.Value = 0;
}
}
}