使用文件对话框选择多个xml文件并保存它们

时间:2014-01-07 16:08:52

标签: c# xml

我有几个xml文件,我使用打开文件对话框选择它们,然后从这些文件中删除重复数据,现在我希望每个文件都保存为.bak文件我可以选择多个文件并从文件中删除这些数据但是删除后没有如何保存这些文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
 using System.IO.Path;

namespace XML_Deletes
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

  //single indivual file
            var doc = XDocument.Load(@"C:\\Users\IT-Administrator\Desktop\21.xml");
            doc.Root.Elements("Incident")
         .GroupBy(s => (string)s.Element("Comment"))
         .SelectMany(g => g.Skip(1))
         .Remove();
//doc.Save(@"C:\Users\IT-Administrator\Desktop\22.xml");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = "XML files(.xml)|*.xml|all Files(*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = true;

            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                {
    if (!String.Equals(Path.GetExtension(openFileDialog1.FileName),
                       ".xml",
                       StringComparison.OrdinalIgnoreCase))
    {
        // Invalid file type selected; display an error.
        MessageBox.Show("The type of the selected file is not supported by this application. You must select an XML file.",
                        "Invalid File Type",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);

    }
    else
    {
    }
}  }
    }

}

1 个答案:

答案 0 :(得分:0)

您在寻找XDocument.Save吗?

doc.Save(@"C:\\Users\IT-Administrator\Desktop\21.xml");

那已经存在于您的代码中(虽然它被注释掉了吗?)。你有保存问题吗?