Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, the problem is i created a winform application that can add data from datatable to xml document and also update, delete. but iam using readxml() and writexml() to read the data from xml and write the data to xml but once if added one row in xml document then if i again wanted to add one row the row is adding with the data and also previous data like repeating the whole datatable and when i opened the application if i deleted one row the xml document becoming error and exceptions are coming plz help me

What I have tried:

C#
private void Form1_Load(object sender, EventArgs e)
{
    dtable.Columns.Add("NAME");
    dtable.Columns.Add("DESIGNATION");
    dtable.Columns.Add("COMPANY");
    dgvxml.DataSource = dtable;
    ds.ReadXml(str);
    ds.WriteXml(str);
    dgvxml.DataSource = ds.Tables[0];
}

private void btnadd_Click(object sender, EventArgs e)
{
    dtable.TableName = "PARTICIPANTS";

    if (a <= 0)
    {
        dtable.ReadXml(str);
        a++;
    }

    DataRow drow = dtable.NewRow();
    drow["NAME"] = txtname.Text;
    drow["DESIGNATION"] = txtdesig.Text;
    drow["COMPANY"] = txtcomp.Text;
    dtable.Rows.Add(drow);
    dgvxml.DataSource = dtable;
    dtable.WriteXml(str);

    txtname.Text = "";
    txtdesig.Text = "";
    txtcomp.Text = "";
    label4.Text = "";
    timer1.Start();
}

private void btndel_Click(object sender, EventArgs e)
{
    try
    {
        if (txtname.Text == "" || txtcomp.Text == "" || txtcomp.Text == "")
        {
            label4.Text = "Select one row";
        }
        else
        {
            int rowindex = dgvxml.CurrentCell.RowIndex;
            dgvxml.Rows.RemoveAt(rowindex);

            dtable.WriteXml(str);

            txtname.Text = "";
            txtdesig.Text = "";
            txtcomp.Text = "";
            label4.Text = "";
            btnadd.Visible = true;
        }
    }
    catch (Exception)
    {

        label4.Text = "Change the Xml file name and proceed";
    }
}
Posted
Updated 31-Aug-17 4:41am
v2
Comments
Sheila Pontes 31-Aug-17 9:13am    
HI,
What does the content in the variable str? What is the error?
Thomas Nielsen - getCore 31-Aug-17 11:46am    
Difficult to state where to start exactly, It would be a good idea to introduce the consept on an identifier or id, this way two participants with the same name is both possible and identifiable.

In your setup what you want to do is to store data in an ado.dataset and datatable and persist it in an xml document. But you do now want to display names if there are duplicates and logically not store them i'm sure.

But then why not use a List and a data class, and linq and say Json as it is less bloated compared to xml. So yea i'd recommend upgrading to newer techniques. this will qive you stuff like distinct for free.

Would you consider a completely different implementation to solve your problem?

1 solution

Hi,


Below, a complete example to help your problem. I used one column in this example.

C#
using System.IO;
using System.Xml;

......

DataSet ds = new System.Data.DataSet();
string str = @"C:\your_path\PARTICIPANTS.xml";

private void btn_delete_Click(object sender, EventArgs e)
{
    ds.Tables["PARTICIPANTS"].Rows[this.dgvxml.CurrentCell.RowIndex].Delete();
    ds.Tables["PARTICIPANTS"].AcceptChanges();
    this.Write_Xml();
}

private void Open_Xml()
{
     //create struct and add table in the dataset
     DataTable dtable = new System.Data.DataTable("PARTICIPANTS");
     dtable.Columns.Add("NAME");
     ds.Merge(dtable);

     //verify if the xml file exist
     if (File.Exists(str))
     {
                //open xml into dataset
                using (Stream stream_xml = new FileStream(str, FileMode.Open, FileAccess.Read))
                {
                    ds.Tables["PARTICIPANTS"].ReadXml(stream_xml);
                }
     }
     else
     { 
                //write new xml document in machine 
                this.Write_Xml();
     }
     dgvxml.DataSource = ds.Tables["PARTICIPANTS"];
}

private void Write_Xml()
{
     XmlTextWriter stwxml = new XmlTextWriter(str, System.Text.Encoding.UTF8);
     ds.Tables["PARTICIPANTS"].WriteXml(stwxml, XmlWriteMode.IgnoreSchema, false);
     stwxml.Close();  
}

private void Form3_Load(object sender, EventArgs e)
{
     //open xml
      this.Open_Xml();            
}

private void btn_add_Click(object sender, EventArgs e)
{
     //add line
      DataRow drow = ds.Tables["PARTICIPANTS"].NewRow();
      drow["NAME"] = txt_name.Text;
      ds.Tables["PARTICIPANTS"].Rows.Add(drow);
      dgvxml.DataSource = ds.Tables["PARTICIPANTS"];

      //write xml
      this.Write_Xml();
}

private void dgvxml_CellClick(object sender, DataGridViewCellEventArgs e)
{
     this.txt_name.Text = ds.Tables["PARTICIPANTS"].Rows[e.RowIndex]["NAME"].ToString();
}
 
Share this answer
 
v3
Comments
Member 13375990 1-Sep-17 2:34am    
tq @sheila pontes my problem solved
Sheila Pontes 1-Sep-17 8:26am    
I'm glad to help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900