使用保存文件对话框保存xml文件

时间:2014-01-08 10:58:55

标签: c# xml

我尝试打开一个带有打开文件对话框的xml文件,想要从文件中删除一些重复数据,现在我的问题是选择文件并保存该文件(加载,删除,保存我的winforms上的按钮)。请你我哪里错了。

public Form1()
{
     InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) // open file dialog works fine
{
     OpenFileDialog openFileDialog1 = new OpenFileDialog();

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

     openFileDialog1.Multiselect = true;

     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
        {

        }


     }
}

private void button2_Click(object sender, EventArgs e)//Deleteing  duplicate data
{
     //var doc = XDocument.Load(@"C:\\Users\IT-Administrator\Desktop\21.xml");/ do i need to use this line.
     doc.Root.Elements("Incident")
     .GroupBy(s => (string)s.Element("Comment"))
     .SelectMany(g => g.Skip(1))
     .Remove();


      //doc.Save(@"C:\Users\IT-Administrator\Desktop\2014-01-07_Middlesex.xml");

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



private void button3_Click(object sender, EventArgs e)//saving..
{
   //doc.Save(@"C:\Users\IT-Administrator\Desktop\22.xml");
   saveFileDialog1.ShowDialog();

}

1 个答案:

答案 0 :(得分:1)

要保存您的XML文件,您必须:

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XML-File | *.xml";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
   xDocument.Save(saveFileDialog.FileName);
}