private void btnListado_Click(object sender, EventArgs e) { try { var objProducto = ClsArregloProducto.leerXml(); if (objProducto != null) { foreach (ClsProducto producto in objProducto.listaProductos) { int i = 0; dgvListado.Rows.Add(); dgvListado.Rows[i].Cells[0].Value = producto.Descripcion; dgvListado.Rows[i].Cells[1].Value = producto.Categoria; dgvListado.Rows[i].Cells[2].Value = producto.Precio; dgvListado.Rows[i].Cells[3].Value = producto.Cantidad; dgvListado.Rows[i].Cells[4].Value = producto.Vencimiento; i++; } } } catch (Exception ex) { MessageBox.Show(ex.StackTrace); } }
public static ClsArregloProducto leerXml() { ClsArregloProducto objArr = null; OpenFileDialog op = new OpenFileDialog(); op.Filter = "Xml file | *.xml"; if (op.ShowDialog() == DialogResult.OK) { using (FileStream file = new FileStream(op.FileName, FileMode.Open)) { XmlSerializer objXml = new XmlSerializer(typeof(ClsArregloProducto)); objArr = (ClsArregloProducto)objXml.Deserialize(file); } } return(objArr); }