国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

c#批量整理xml格式示例

2020-01-24 02:46:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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 結(jié)尾的,可以寫成 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++)
            {
                // 獲取每個(gè)文件名,并記錄到 字符串 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 標(biāo)簽上
            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;
        }

    }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洛宁县| 莱芜市| 双流县| 襄樊市| 达日县| 加查县| 昭觉县| 襄樊市| 清涧县| 玛沁县| 平阳县| 鄂尔多斯市| 敦化市| 盘山县| 衡南县| 都兰县| 收藏| 休宁县| 广东省| 稻城县| 乌恰县| 普格县| 平度市| 奉新县| 贵溪市| 宁波市| 鄂尔多斯市| 巴里| 灌云县| 佳木斯市| 双江| 凤冈县| 邵武市| 皋兰县| 辽源市| 祁阳县| 靖西县| 翼城县| 北票市| 八宿县| 珠海市|