2014-01-07 52 views
0

HI我有幾個XML文件,我選擇它們使用打開的文件對話框,然後從這些文件中刪除重複的數據,現在我想每個單獨的文件保存爲as.bak文件我可以選擇多個文件並從文件中刪除這些數據,但不知道如何在刪除後保存這些文件。使用文件對話框選擇多個XML文件並保存它們

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

}

回答

0

您是否在尋找XDocument.Save

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

這已經在你的代碼中(雖然它的註釋掉了?)。你有問題保存嗎?

+0

我想保存與samenamebak.xml更新的文件我不知道該怎麼做。上面的模塊保存單個文件。 – preethi

+0

如何在var doc = XDocument.Load(@「C:\\ Users \ IT-Administrator \ Desktop \ 21.xml」)後使用savefile對話框; .SelectMany(g => g.Skip(1)) .Remove(); doc.Root.Elements(「Incident」) .GroupBy(s =>(string)s.Element(「Comment」)) ' – preethi

+0

如何使用保存文件daialog保存xml文件。 – preethi

相關問題