欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c#批量整理xml格式示例

时间:2021-06-07 08:54:56|栏目:.NET代码|点击:

复制代码 代码如下:

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

    }
}

上一篇:C#编程实现向并口设备发送指令、获取并口设备的状态

栏    目:.NET代码

下一篇:浅谈C#中的for循环与foreach循环

本文标题:c#批量整理xml格式示例

本文地址:http://www.codeinn.net/misctech/137155.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有