0

i am trying to insert the value from control into xml but the record is getting overwriting with the previous one that is only one entry remains in xml file.plz give me solution,my code is like:

namespace StudentApplicationForm
{

 public class information

 {

private String txtbox1;

 private String txtbox2;

 private String txtbox3;

 public String StudentName

 {

  get { return txtbox1; }

 set{  txtbox1 = value; }
}

        public String StudentId
        {
            get { return txtbox2; }
            set{ txtbox2 = value; }
        }
     public String StudentBranch
        {
            get { return txtbox3; }
            set { txtbox3 = value; }
        }


    }

}//getter and setter methods



and the file actual insert logic is written is:


 public void ok_Click(object sender, EventArgs e)
        {
            try

            {

    information info = new information();

    List<information> i1 = new List<information>();

      XmlSerializer serial = new XmlSerializer(typeof(List<information>));

                info.StudentName = textBox1.Text;//id of control

                info.StudentId = textBox2.Text;

             if (radioButton1.Checked)

                 {
                    info.StudentBranch = radioButton1.Text;
                 }

               if (radioButton2.Checked)

                  {

                   info.StudentBranch = radioButton2.Text;
              }
                  if (radioButton3.Checked)
         {
             info.StudentBranch = radioButton3.Text;
          }

        i1.Add(info);

 using (FileStream fs = newFileStream(Environment.CurrentDirectory + "\\mynew.xml", FileMode.Create, FileAccess.Write))

         {
                   serial.Serialize(fs, i1);
                    MessageBox.Show("Created");
      }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
1
  • Can you provide us a sample of expected and actual xml file Commented Sep 24, 2018 at 11:13

1 Answer 1

1

Are you tried like this,

 i1.Add(new information{ StudentName = info.StudentName, StudentId = info.StudentId,  StudentBranch = info.StudentBranch});
Sign up to request clarification or add additional context in comments.

1 Comment

@PriyankaYemul, Then try like this. You are not created object in your list. That is the problem in your code.

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.