c#批量整理xml格式示例

 更新时间:2014年03月04日 16:49:46   作者:  
这篇文章主要介绍了c#批量整理xml格式示例,win7的x64和x86系统下已验证通过,需要的朋友可以参考下

复制代码 代码如下:

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#实现对象XML序列化的方法

    C#实现对象XML序列化的方法

    这篇文章主要介绍了C#实现对象XML序列化的方法,是C#常见的实用技巧,需要的朋友可以参考下
    2014-11-11
  • 如何在Mac系统使用Visual Studio Code运行Python

    如何在Mac系统使用Visual Studio Code运行Python

    这篇文章主要介绍了Mac使用Visual Studio Code运行Python环境的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • 在winform中嵌入第三方软件窗体的实践分享

    在winform中嵌入第三方软件窗体的实践分享

    这篇文章主要介绍了在winform中如何嵌入第三方软件窗体的实践分享,文中通过代码示例和图文给大家介绍的非常详细,具有一定参考价值,需要的朋友可以参考下
    2024-03-03
  • C#使用throw和throw ex的区别小结

    C#使用throw和throw ex的区别小结

    本文主要介绍了C#使用throw和throw ex的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-04-04
  • WPF实现轮播图效果(图片、视屏)

    WPF实现轮播图效果(图片、视屏)

    这篇文章主要介绍了WPF实现轮播图效果,以下是一个使用WPF技术实现图片和视屏轮播的简单案例代码示例,文中有详细的代码示例,具有一定的参考价值,感兴趣的小伙伴可以自己动手试试
    2023-10-10
  • C#实现监听串口数据的方法详解

    C#实现监听串口数据的方法详解

    这篇文章主要为大家详细介绍了C#实现监听串口数据的相关方法,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
    2024-03-03
  • C#使用WMI实现监听进程的启动和关闭

    C#使用WMI实现监听进程的启动和关闭

    Windows Management Instrumentation(WMI)是用于管理基于 Windows 操作系统的数据和操作的基础结构,本文将使用WMI实现监听进程的启动和关闭,感兴趣的可以了解下
    2024-01-01
  • C#实现按照指定长度在数字前补0方法小结

    C#实现按照指定长度在数字前补0方法小结

    这篇文章主要介绍了C#实现按照指定长度在数字前补0方法,实例总结了两个常用的数字补0的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C# Winform自动更新程序实例详解

    C# Winform自动更新程序实例详解

    这篇文章主要为大家详细介绍了C# Winform 自动更新程序实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • C#使用FolderBrowserDialog类实现选择打开文件夹方法详解

    C#使用FolderBrowserDialog类实现选择打开文件夹方法详解

    这篇文章主要介绍了C#选择文件夹/打开文件夹/浏览文件夹等代码方法,大家参考使用
    2013-11-11

最新评论