I want to generate an xml file in the fllowing format.
Among other problems, the main one is here:
data.SalesInvoice = new[] { sales };
You create the SalesInvoice array from scratch at evey cycle loop, instead of creating it before the cycle and adding elements inside it.
Here's how I would do (look at variable names too please):
//Plural as it is a list
List salesInvoices = new List();
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
//Singular as it is a single element
var salesInvoice = new SalesInvoice();
salesInvoice.DocNo = dataGridView1.Rows[i].Cells[0].FormattedValue.ToString();
salesInvoice.Item = dataGridView1.Rows[i].Cells[1].FormattedValue.ToString();
salesInvoice.Qty = dataGridView1.Rows[i].Cells[2].FormattedValue.ToString();
salesInvoice.Price = dataGridView1.Rows[i].Cells[3].FormattedValue.ToString();
salesInvoices.Add(salesInvoice);
//What's the purpose of the next line?
var serializer1 = new XmlSerializer(typeof(SalesInvoice));
}
// I pluralized this property name too
data.SalesInvoices = salesInvoices.ToArray();