To pass more than one row value from database to XML

后端 未结 2 1637
再見小時候
再見小時候 2021-01-22 02:31

I want to generate an xml file in the fllowing format.

      
      

        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-22 03:31

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

提交回复
热议问题