c#批量整理xml格式示例
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Xml;
usingSystem.IO;
namespaceWindowsApplication1
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(listBox1.Items.Count==0)
{
MessageBox.Show("nofilename");
}
else
{
func_SearchFiles(sender,e);//取得文件名
}
//listBox1.Items.Clear();
}
privatevoidForm1_DragDrop(objectsender,DragEventArgse)
{
stringpath=((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
listBox1.Items.Add(path);//显示文件夹目录
}
privatevoidForm1_DragEnter(objectsender,DragEventArgse)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect=DragDropEffects.Link;
else
e.Effect=DragDropEffects.None;
}
privatevoidfunc_SearchFiles(objectsender,EventArgse)
{
//获取指定文件夹目录
stringfilepath=listBox1.Items[0].ToString();
DirectoryInfobaseDir=newDirectoryInfo(filepath);
//获取指定文件夹下的所有文件。
//如果你需要获取特定格式的文件,如.html结尾的,可以写成baseDir.GetFiles("*.html");
FileInfo[]files=baseDir.GetFiles("*.xml");
//定义文件名字符串
progressBar1.Visible=true;
progressBar1.Maximum=files.Length;
progressBar1.Minimum=0;
stringfileNames=string.Empty;
for(inti=0;i<files.Length;i++)
{
//获取每个文件名,并记录到字符串fileNames里
//如果需要获取文件的完整路径名,files[i].FullName;
//fileNames+=files[i].FullName+",";
stringxmlfile=@files[i].FullName;
MemoryStreammstream=newMemoryStream(1024);
XmlTextWriterwriter=newXmlTextWriter(mstream,null);
XmlDocumentxmldoc=newXmlDocument();
writer.Formatting=Formatting.Indented;
xmldoc.Load(xmlfile);
xmldoc.WriteTo(writer);
writer.Flush();
writer.Close();
Encodingencoding=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";
//Filemyfile=newfile
xmldoc.Save(@files[i].FullName);
mstream.Close();
}
//显示到Label标签上
listBox1.Items.Add("Finish!!!!");
listBox1.SelectedIndex=listBox1.Items.Count-1;
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
//this.listBox1.SelectedItem=listBox1.Items.IndexOf(0);//保持文本显示在最后一行
listBox1.Items.Clear();
progressBar1.Visible=false;
progressBar1.Value=0;
}
}
}
相关文章
- c#数组赋初值_C#数组初始化
- C# 解析XML「建议收藏」
- asp.net员工管理系统VS开发sqlserver数据库web结构c#编程计算机网页项目
- C#性能测试BenchmarkDotnet
- c# 多线程并发-金三银四面试:C#.NET面试题高级篇2-多线程
- C#中隐式运行CMD命令行窗口的方法
- C#给picturebox控件加图片选中状态的2个方法
- C#解析json文件的实现代码
- C#Hashtable/Dictionary写入和读取对比详解
- C#波形图控件制作示例程序
- C#事件访问器详解
- C#中的switchcase使用介绍
- C#使用SqlBulkCopy批量复制数据到数据表
- C#字符串的截取函数用法总结
- asp.net中c#自定义事件的实现方法详解
- C#定时器实现自动执行的方法
- C#获取变更过的DataTable记录的实现方法
- C#后台创建控件并获取值的方法
- C#实现洗牌游戏实例
- C#中string和StingBuilder内存中的区别实例分析