Priyanka Yemul
Priyanka Yemul

Reputation: 1

dynamically adding value entered into different control into xml using c#

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);
            }
        }

Upvotes: 0

Views: 51

Answers (1)

suresh
suresh

Reputation: 65

Are you tried like this,

 i1.Add(new information{ StudentName = info.StudentName, StudentId = info.StudentId,  StudentBranch = info.StudentBranch});

Upvotes: 1

Related Questions