2014-01-08 152 views
2

我試圖用打開的文件對話框打開一個xml文件,並且想從文件中刪除一些重複數據現在我的問題是選擇文件並保存該文件(加載,刪除,保存buttton on我的winforms)。你可以請我哪裏錯了。使用保存文件對話框保存xml文件

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

要保存XML的文件,你必須:

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

感謝TomTom公司的快速反應..但我在選擇文件,而刪除的問題,我需要用這種說法,同時刪除重複記錄'var doc = XDocument.Load(@「C:\\ Users \ IT-Administrator \ Desktop \ 21.xml」)' – preethi

+0

當我嘗試你的查詢時,我得到這個錯誤..'對象引用是必需的非靜態字段,方法或屬性'System.Xml.Linq.XDocument.Save(string)'' – preethi

+0

我一直在收到@preethi的錯誤消息是否解決了它? – Chris