zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c#批量整理xml格式示例

c#批量XML 示例 格式 整理
2023-06-13 09:15:19 时间

复制代码代码如下:


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;
       }

   }
}