WriteAll() public method

Write all records to the output CSV stream.
public WriteAll ( string records ) : void
records string an array of string arrays containing the /// records to be written. Each string becomes a single comma separated /// field, each array of strings becomes a record.
return void
Beispiel #1
0
        public void TestCsvWriter()
        {
            string     filename  = MixedTestFile;
            FileStream inStream  = null;
            FileStream outStream = null;

            try
            {
                Console.WriteLine("Loading " + filename);
                inStream = File.OpenRead(filename);
                CsvReader  reader  = new CsvReader(inStream);
                string[][] records = reader.ReadAll();

                string outName = Path.Combine(CsvOutputDirectory, "test-writer.csv");
                outStream = File.OpenWrite(outName);
                outStream.SetLength(0L);

                CsvWriter writer = new CsvWriter(outStream);
                //writer.QuoteLimit = -1;

                writer.WriteAll(records);
                outStream.Flush();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                if (inStream != null)
                {
                    inStream.Close();
                }

                if (outStream != null)
                {
                    outStream.Close();
                }
            }
        }
        public void TestCsvWriter()
        {
            const string filename = CsvTestDataDirectory + "mixed.csv";
            FileStream inStream = null;
            FileStream outStream = null;
            try
            {
                Console.WriteLine( "Loading " + filename );
                inStream = File.OpenRead( filename );
                CsvReader reader = new CsvReader( inStream );
                string [][] records = reader.ReadAll();

                const string outName = CsvOutputDirectory + "test-writer.csv";
                outStream = File.OpenWrite( outName );
                outStream.SetLength( 0L );

                CsvWriter writer = new CsvWriter( outStream );
                //writer.QuoteLimit = -1;

                writer.WriteAll( records );
                outStream.Flush();
            }
            catch (Exception ex)
            {
                Assert.Fail( ex.Message );
            }
            finally
            {
                if( inStream != null )
                {
                    inStream.Close();
                }

                if( outStream != null )
                {
                    outStream.Close();
                }
            }
        }
Beispiel #3
0
        public void TestWriteAlternateQuote()
        {
            string filename = Path.Combine(CsvOutputDirectory, "test-write-alternate-quote.csv");

            string[][] recordsOut =
            {
                new string[] { "aaa",  "bb*b",                               "ccc"       },
                new string[] { "",     "new" + Environment.NewLine + "line", "quoted"    },
                new string[] { "with", "\"other\"",                          "quo\"\"te" }
            };

            Stream stream = null;

            try
            {
                Console.WriteLine("Creating " + filename);
                //	Create the temp file (or overwrite if already there).
                stream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                stream.SetLength(0);

                //	Append the data.
                CsvWriter writer = new CsvWriter(stream);
                writer.Quote      = '*';
                writer.QuoteLimit = -1;
                writer.WriteAll(recordsOut);
                stream.Flush();
                stream.Close();

                Console.WriteLine("Loading " + filename);
                stream = File.OpenRead(filename);
                CsvReader reader = new CsvReader(stream);
                reader.Quote = '*';
                string[][] recordsIn = reader.ReadAll();

                int line = 0;
                foreach (string[] record in recordsIn)
                {
                    Console.WriteLine(++line + ":" + ToString(record));
                }

                Assert.IsTrue(recordsIn.Length == 3, "Wrong number of records in " + filename);

                int index = 0;
                Assert.IsTrue(recordsIn[index].Length == 3, "Wrong number of items on record " + (index + 1));
                Assert.IsTrue(CompareStringArray(recordsOut[index], recordsIn[index]), "contents of record " + (index + 1));

                index++;
                Assert.IsTrue(recordsIn[index].Length == 3, "Wrong number of items on record " + (index + 1));
                Assert.IsTrue(CompareStringArray(recordsOut[index], recordsIn[index]), "contents of record " + (index + 1));

                index++;
                Assert.IsTrue(recordsIn[index].Length == 3, "Wrong number of items on record " + (index + 1));
                Assert.IsTrue(CompareStringArray(recordsOut[index], recordsIn[index]), "contents of record " + (index + 1));
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
        public void TestWriteAlternateQuote()
        {
            string filename = Path.Combine(CsvOutputDirectory, "test-write-alternate-quote.csv");
            string[][] recordsOut =
            {
                new string[] { "aaa", "bb*b", "ccc" },
                new string[] { "", "new" + Environment.NewLine + "line", "quoted" },
                new string[] { "with", "\"other\"", "quo\"\"te" }
            };

            Stream stream = null;
            try
            {
                Console.WriteLine("Creating " + filename);
                //	Create the temp file (or overwrite if already there).
                stream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                stream.SetLength(0);

                //	Append the data.
                CsvWriter writer = new CsvWriter(stream);
                writer.Quote = '*';
                writer.QuoteLimit = -1;
                writer.WriteAll(recordsOut);
                stream.Flush();
                stream.Close();

                Console.WriteLine("Loading " + filename);
                stream = File.OpenRead(filename);
                CsvReader reader = new CsvReader(stream);
                reader.Quote = '*';
                string[][] recordsIn = reader.ReadAll();

                int line = 0;
                foreach (string[] record in recordsIn)
                {
                    Console.WriteLine(++line + ":" + ToString(record));
                }

                Assert.IsTrue(recordsIn.Length == 3, "Wrong number of records in " + filename);

                int index = 0;
                Assert.IsTrue(recordsIn[index].Length == 3, "Wrong number of items on record " + (index + 1));
                Assert.IsTrue(CompareStringArray(recordsOut[index], recordsIn[index]), "contents of record " + (index + 1));

                index++;
                Assert.IsTrue(recordsIn[index].Length == 3, "Wrong number of items on record " + (index + 1));
                Assert.IsTrue(CompareStringArray(recordsOut[index], recordsIn[index]), "contents of record " + (index + 1));

                index++;
                Assert.IsTrue(recordsIn[index].Length == 3, "Wrong number of items on record " + (index + 1));
                Assert.IsTrue(CompareStringArray(recordsOut[index], recordsIn[index]), "contents of record " + (index + 1));
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
        public override void Save( string filename )
        {
            //FileInfo info = new FileInfo( filename );
            Debug.WriteLine("Saving " + filename);

            FileStream outStream = null;
                try
                {
                    outStream = File.OpenWrite(filename);
                    outStream.SetLength( 0L );

                    CsvWriter writer = new CsvWriter( outStream );
                    //writer.QuoteLimit = -1;
                    CsvDocument csvDoc = this.CsvDocument;

                    writer.WriteAll( csvDoc.Rows );
                    outStream.Flush();
                }
                finally
                {
                    if( outStream != null )
                    {
                        outStream.Close();
                    }
                }

            base.Save( filename );
        }