public static string SerializeDataRow(this System.Data.DataRow row, System.Data.XmlWriteMode shema = System.Data.XmlWriteMode.WriteSchema) { System.Data.DataTable tabletosend = null; if (row.Table.Rows.Count > 1) { tabletosend = row.Table.Clone(); tabletosend.Rows.Add(row); } else { tabletosend = row.Table; } return(SerializeDataTable(tabletosend, shema)); }
/* * /// <summary> * /// Gets a Inverted DataTable * /// https://www.codeproject.com/Articles/22008/C-Pivot-Table * /// </summary> * /// <param name="table">Provided DataTable</param> * /// <param name="columnX">X Axis Column</param> * /// <param name="columnY">Y Axis Column</param> * /// <param name="columnZ">Z Axis Column (values)</param> * /// <param name="columnsToIgnore">Whether to ignore some column, it must be * /// provided here</param> * /// <param name="nullValue">null Values to be filled</param> * /// <returns>C# Pivot Table Method - Felipe Sabino</returns> * public static DataTable PivotInversedTable(DataTable table, string columnX, * string columnY, string columnZ, string nullValue, bool sumValues) * { * //Verify if Y and Z Axis columns re provided * if (string.IsNullOrEmpty(columnY) || string.IsNullOrEmpty(columnZ)) * throw new Exception("The columns to perform inversion are not provided"); * * //Create a DataTable to Return * DataTable returnTable = new DataTable(); * if (columnX == "") columnX = table.Columns[0].ColumnName; * returnTable.Columns.Add(columnY); * //Read all DISTINCT values from columnX Column in the provided DataTale * List<string> columnXValues = table.Rows.ToList().Select(row => Nglib.DATA.COLLECTIONS.DataSetTools.GetRowString(row, columnX)).Distinct().ToList(); * * * //Read DISTINCT Values for Y Axis Column * List<string> columnYValues = new List<string>(); * foreach (DataRow dr in table.Rows) * if (!columnYValues.Contains(dr[columnY].ToString())) * columnYValues.Add(dr[columnY].ToString()); * * * //Loop all Column Y Distinct Value * foreach (string columnYValue in columnYValues) * { * //Creates a new Row * DataRow drReturn = returnTable.NewRow(); * drReturn[0] = columnYValue; * //foreach column Y value, The rows are selected distincted * DataRow[] rows = table.Select(columnY + "='" + columnYValue + "'"); * * //Read each row to fill the DataTable * foreach (DataRow dr in rows) * { * string rowColumnTitle = dr[columnX].ToString(); * * //Read each column to fill the DataTable * foreach (DataColumn dc in returnTable.Columns) * { * if (!dc.ColumnName.Equals(rowColumnTitle)) * continue; * * //If Sum of Values is True it try to perform a Sum * //If sum is not possible due to value types, the value * // displayed is the last one read * if (sumValues) * { * try * { * drReturn[rowColumnTitle] = * Convert.ToDecimal(drReturn[rowColumnTitle]) + * Convert.ToDecimal(dr[columnZ]); * } * catch * { * drReturn[rowColumnTitle] = dr[columnZ]; * } * } * else * { * drReturn[rowColumnTitle] = dr[columnZ]; * } * * } * } * returnTable.Rows.Add(drReturn); * } * * * //if a nullValue is provided, fill the datable with it * if (nullValue != "") * foreach (DataRow dr in returnTable.Rows) * foreach (DataColumn dc in returnTable.Columns) * if (dr[dc.ColumnName].ToString() == "") * dr[dc.ColumnName] = nullValue; * * * return returnTable; * } */ #endregion public static string SerializeDataTable(this System.Data.DataTable ret, System.Data.XmlWriteMode shema = System.Data.XmlWriteMode.WriteSchema) { try { using (MemoryStream stream = new MemoryStream()) { ret.WriteXml(stream, shema); stream.Position = 0; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { return(reader.ReadToEnd()); } } } catch (Exception ex) { throw new Exception("SerializeDataTable " + ex.Message, ex); } }