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 結(jié)尾的,可以寫成 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 標(biāo)簽上
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;
}
}
}
相關(guān)文章
C# Winform使用擴(kuò)展方法實現(xiàn)自定義富文本框(RichTextBox)字體顏色
這篇文章主要介紹了C# Winform使用擴(kuò)展方法實現(xiàn)自定義富文本框(RichTextBox)字體顏色,通過.NET的靜態(tài)擴(kuò)展方法來改變RichTextBox字體顏色,需要的朋友可以參考下2015-06-06C#獲取變更過的DataTable記錄的實現(xiàn)方法
這篇文章主要介紹了C#獲取變更過的DataTable記錄的實現(xiàn)方法,對初學(xué)者很有學(xué)習(xí)借鑒價值,需要的朋友可以參考下2014-08-08C#中數(shù)組Array,ArrayList,泛型List詳細(xì)對比
關(guān)于數(shù)組Array,ArrayList,泛型List,簡單的說數(shù)組就是值對象,它存儲數(shù)據(jù)元素類型的值的一系列位置.Arraylist和list可以提供添加,刪除,等操作的數(shù)據(jù). 具體如何進(jìn)行選擇使用呢,我們來詳細(xì)探討下2016-06-06精簡高效的C#網(wǎng)站優(yōu)化經(jīng)驗技巧總結(jié)
這篇文章主要為大家介紹了精簡高效的C#網(wǎng)站優(yōu)化經(jīng)驗技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04Entity?Framework映射TPH、TPT、TPC與繼承類
這篇文章介紹了Entity?Framework映射TPH、TPT、TPC與繼承類,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06DevExpress實現(xiàn)根據(jù)行,列索引來獲取RepositoryItem的方法
這篇文章主要介紹了DevExpress實現(xiàn)根據(jù)行,列索引來獲取RepositoryItem的方法,需要的朋友可以參考下2014-08-08c#關(guān)于JWT跨域身份驗證的實現(xiàn)代碼
這篇文章主要介紹了c#關(guān)于JWT跨域身份驗證的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10c#根據(jù)文件類型獲取相關(guān)類型圖標(biāo)的方法代碼
c#根據(jù)文件類型獲取相關(guān)類型圖標(biāo)的方法代碼,需要的朋友可以參考一下2013-05-05