Here you can fill a Data table dynamically with the results of Xml query (Prev. Post)
var dtRowsElements = objData.SelectAllfromTable(ElementTable);
IEnumerable
DataTable objDTable = objData.FillDataTable(dtRows);
/*Function to create the Data Table.*/
public DataTable FillDataTable(IEnumerable
{
/*Crea Datatable a partir de lo que Elementos obtenidos de dtRows "Consulta XML que es un IEnumerable
DataTable dtDescrValue = new DataTable();
try
{
foreach (XElement dtColumn in dtRows.First().Elements())
{
//Crea Columnas
dtDescrValue.Columns.Add(dtColumn.Name.ToString(), typeof(string));
}
foreach (XElement dtRow in dtRows)
{
//Crea cada registro "Row"
DataRow dtDescrRow = dtDescrValue.NewRow();
foreach (XElement dtColumn in dtRows.First().Elements())
{ //Asigna valores de cada column de "Row"
dtDescrRow[dtColumn.Name.ToString()] = dtRow.Element(dtColumn.Name.ToString()).Value;
}
dtDescrValue.Rows.Add(dtDescrRow);
}
}
catch (Exception e)
{//En caso de Error regresa un Data table con Id: "Error", Descr: Message de error.
dtDescrValue.Columns.Add("Descr", typeof(string));
dtDescrValue.Columns.Add("Id", typeof(string));
DataRow dtDescrRow = dtDescrValue.NewRow();
dtDescrRow["Descr"] = e.Message;//"No data...";
dtDescrRow["Id"] = "Error";
dtDescrValue.Rows.Add(dtDescrRow);
}
return dtDescrValue;
}
No hay comentarios:
Publicar un comentario