Esempio n. 1
0
        /// <summary>
        /// Reads a record set file and outputs a CSV.
        /// </summary>
        /// <param name='inStream'>
        /// The stream for the record set.
        /// </param>
        /// <param name='outWriter'>
        /// Where to write the CSV.
        /// </param>
        /// <param name="excludeOrigColumns">
        /// Should the output include the original values as well as the standarized ones
        /// </param>
        public static void WriteCSV(Stream inStream, TextWriter outWriter, bool excludeOrigColumns)
        {
            var serializer = new XmlSerializer(typeof(RecordSet));
            var records    = (RecordSet)serializer.Deserialize(inStream);
            var table      = RecordHelper.BuildTableOfRecords(records);

            if (excludeOrigColumns)
            {
                var collectionFieldsToRemove = new List <DataColumn>();
                foreach (var collectionField in table.Columns.Cast <DataColumn>().Where(x => x.ColumnName.EndsWith("_ORIG")))
                {
                    if (
                        table.Columns.Cast <DataColumn>().Any(
                            x =>
                            x.ColumnName ==
                            collectionField.ColumnName.Substring(0, collectionField.ColumnName.Length - 5) + "_STD" ||
                            x.ColumnName == collectionField.ColumnName.Substring(0, collectionField.ColumnName.Length - 5)))
                    {
                        collectionFieldsToRemove.Add(collectionField);
                    }
                }

                foreach (var dataColumn in collectionFieldsToRemove)
                {
                    table.Columns.Remove(dataColumn);
                }
            }

            var columnCount = table.Columns.Count;

            foreach (DataColumn column in table.Columns)
            {
                outWriter.Write(column.ColumnName);
                if (--columnCount > 0)
                {
                    outWriter.Write(",");
                }
            }
            outWriter.WriteLine();
            foreach (DataRow row in table.Rows)
            {
                columnCount = table.Columns.Count;
                foreach (DataColumn column in table.Columns)
                {
                    outWriter.Write(row [column]);
                    if (--columnCount > 0)
                    {
                        outWriter.Write(",");
                    }
                }
                outWriter.WriteLine();
            }

            // Ensure all the buffered data is written before finishing
            outWriter.Flush();
        }