4

I tried to open an xml file with open file dialog and want to removed some duplicate data from the file now my problem is selecting the file and saving that file (load, delete,save buttton on my winforms).can you please where am I going wrong.

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 Answer 1

2

To save your XML-File you have to:

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XML-File | *.xml";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
   xDocument.Save(saveFileDialog.FileName);
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks tomtom for quick response .. but my problem in selecting the file while deleting do I need to to use this statement while deleting the duplicate records var doc = XDocument.Load(@"C:\\Users\IT-Administrator\Desktop\21.xml")
when i tried your query i get this error..An object reference is required for the non-static field, method, or property 'System.Xml.Linq.XDocument.Save(string)'
I keep receiving that error message @preethi did you fix it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.