欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#批量整理xml格式示例

程序员文章站 2023-12-20 11:54:22
复制代码 代码如下:using system;using system.collections.generic;using system.componentmodel;us...

复制代码 代码如下:

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

    }
}

上一篇:

下一篇: