Demir Acar

Demir Acar

  • NA
  • 98
  • 25.3k

export datagridview to xml

May 18 2014 12:04 PM
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

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

Answers (3)