Skip to content

Export datagridview to xml

Demir Acar

Asked May 18, 2014, 8:04 AM EDT3 answers
Share
LinkedInX
hi dear ;
At firstly I am  fill to datagrid manually and  save as Xml ,folowing this code,But if I fill to multiple rows in datagrid and then only export last rows

Java
for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
XmlTextWriter xmlDocument = new XmlTextWriter(@"d:\Deneme.xml", System.Text.Encoding.GetEncoding("ISO-8859-9"));
            xmlDocument.Formatting = Formatting.Indented;
            xmlDocument.Indentation = 5;
           try
            {
                xmlDocument.WriteStartDocument();
                xmlDocument.WriteStartElement("SALES_INVOICES");
                xmlDocument.WriteElementString("TYPE", dataGridView1.Rows[i].Cells[0].Value.ToString());
               xmlDocument.WriteEndElement();
                xmlDocument.Close();
                MessageBox.Show("Xml Created.");
            }
            catch (Exception ex)
            {
                  MessageBox.Show("Hata" + ex.Message);
            }
        }

And Result is 

<?xml version="1.0" encoding="iso-8859-9"?>
<SALES_INVOICES>
     <TYPE>1453</TYPE>
</SALES_INVOICES>

I want to result is ;
<?xml version="1.0" encoding="iso-8859-9"?>
<SALES_INVOICES>
     <TYPE>1452</TYPE>
</SALES_INVOICES>
     <TYPE>1453</TYPE>
</SALES_INVOICES>

Thanks in advance

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Umesh Kumar
    0
    If u are using dataset to bind 
    Use Below Code
    Code
    ds.WriteXml("d:\\employee.xml");
    0
  • 0
    I fill to datagrid from excel or enter data manually ..
    0
  • Umesh Kumar
    0
    You can get the datasource of the grid to a dataset and use the code above
    0