Beispiel #1
0
        public bool SimpleCSVDeserialization(SimpleCSVReader reader, SimpleCSVSerializationState state, object handler)
        {
            if (state == SimpleCSVSerializationState.BeforeDeserialization)
            {
                reader[1] = (handler as CartoonDataHandler).MapGroupToID(reader[1]).ToString();
                reader[5] = ("yes".Equals(reader[5])).ToString();
            }

            return true;
        }
Beispiel #2
0
        static void Main(string[] args)
        {

            #region Simple example

            using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\simple.csv"))
            {
                writer.MaxColumns = 10;
                writer.QuoteAll = true;
                writer.Splitter = ',';
                writer.WriteHeader(new string[] { "A", "B", "C", "D" });
                writer.WriteLine(new string[] { "1", "2", "3", "4" });

                for (int i = 1; i < 5; i++)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        writer.Write(i + ":" + j);
                    }
                    writer.WriteLine();
                }
            }

            using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\simple2.csv"))
            {
                writer.MaxColumns = 10;
                writer.QuoteAll = true;
                writer.Splitter = ',';
                writer.WriteHeader(new string[] { "A", "B", "C", "D" });
                writer.WriteLine(new string[] { "1", "2", "3", "4" });

                for (int i = 1; i < 5; i++)
                {
                    for (int j = 20; j >= 0; j--)
                    {
                        writer[j] = i + ":" + j;
                    }
                    writer["A"] = "String indexer";
                    writer.WriteLine();
                }
            }

            using (SimpleCSVReader reader = new SimpleCSVReader(@".\test.txt"))
            {
                reader.Splitter = '\t';
               
                reader.ReadHeader();

                Dictionary<string, int> i = reader.HeaderMap;
                foreach (var item in i)
                {
                    string columnName = item.Key.ToString();
                    string intS = item.Value.ToString();
                    
                }
                while (reader.ReadLine())
                {
                    Console.WriteLine("{0} - {1}", reader["A"], reader[5]);
                }
            }

            #endregion

            #region Serialization Example

            using (SimpleCSVSerializer<MyObject> serializer = new SimpleCSVSerializer<MyObject>())
            {
                using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\serialized.csv"))
                {
                    writer.HasHeader = true;
                    writer.QuoteAll = true;
                    // writer.Splitter = ',';
                    serializer.Serialize(writer, new MyObject
                    {
                        ID = 1,
                        Title = @"The title is ""The title""",
                        Data = "This is an example;wow!",
                        Date = DateTime.Now,
                        NotForExport = "My specified data"
                    });
                    serializer.Serialize(writer, new MyObject
                    {
                        ID = 2,
                        Date = DateTime.Now,
                        NotForExport = "No more!"
                    });
                }

                Console.WriteLine("--- CSV DESERIALIZATION RESULTS --");

                using (SimpleCSVReader reader = new SimpleCSVReader(@".\serialized.csv"))
                {
                    reader.HasHeader = true;
                    // reader.Splitter = ',';
                    MyObject record;

                    while (serializer.DeserializeLine(reader, out record))
                    {
                        Console.WriteLine("{0}-{1}-{2}-{3}", record.ID, record.Title, record.Date, record.Data);
                    }
                }

                Console.WriteLine("--- CSV DESERIALIZATION RESULTS with IEnumerable --");

                using (SimpleCSVReader reader = new SimpleCSVReader(@".\serialized.csv"))
                {
                    reader.HasHeader = true;
                    //reader.Splitter = ',';

                    foreach (var record in serializer.Deserialize<MyObject>(reader))
                    {
                        Console.WriteLine("{0}-{1}-{2}-{3}", record.ID, record.Title, record.Date, record.Data);
                    }
                }
            }

            #endregion

            #region Advanced Serialization Example

            var dataSource = new CartoonDataHandler();

            using (SimpleCSVSerializer<CartoonItem> serializer = new SimpleCSVSerializer<CartoonItem>(dataSource))
            {
                using (SimpleCSVReader reader = new SimpleCSVReader("cartoon.csv"))
                {
                    reader.HasHeader = true;
                    using (SimpleCSVWriter writer = new SimpleCSVWriter("new_cartoon.csv"))
                    {
                        writer.HasHeader = true;

                        foreach (var cartoon in serializer.Deserialize<CartoonItem>(reader))
                        {
                            Console.WriteLine("{0} {1} {2} {3} {4}", cartoon.ID, cartoon.GroupID, cartoon.Created, cartoon.Filmed, cartoon.Name);
                            serializer.Serialize(writer, cartoon);
                        }
                    }
                }

                Console.WriteLine("-- ONLY FILMED --");
                using (SimpleCSVReader reader = new SimpleCSVReader("new_cartoon.csv"))
                {
                    reader.HasHeader = true;
                    serializer.OnDeserialization += (sender, eventArgs) =>
                    {
                        if (eventArgs.State == SimpleCSVSerializationState.BeforeDeserialization)
                        {
                            return "yes".Equals(eventArgs.CSVStream["FILMED"]);
                        }
                        return true;
                    };

                    foreach (var cartoon in serializer.Deserialize <CartoonItem>(reader))
                    {
                        Console.WriteLine("{0} {1} {2} {3} {4} {5}", cartoon.ID, cartoon.GroupID, cartoon.Created, cartoon.Filmed, cartoon.Name, cartoon.GroupType);
                    }
                }
            }

            Console.WriteLine("-- RAW --");
            using (SimpleCSVReader reader = new SimpleCSVReader("new_cartoon.csv"))
            {
                reader.HasHeader = true;

                while (reader.ReadLine())
                {
                    Console.WriteLine("{0} {1} {2} {3} {4} {5}", reader["ID"], reader["GROUP"], reader[3], reader[4], reader[5], reader[6]);
                }
            }

            #endregion

            Console.ReadKey();
        }
Beispiel #3
0
        public void MyThirdCSVRecordTest()
        {
            // serialization with no header
            using (SimpleCSVSerializer<MyThirdCSVRecord> serializer = new SimpleCSVSerializer<MyThirdCSVRecord>())
            {
                using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\MyThirdCSVRecord.csv"))
                {
                    writer.HasHeader = true;
                    foreach (var record in _myThirdCSVRecord)
                    {
                        serializer.Serialize(writer, record);
                    }
                }

                using (SimpleCSVReader reader = new SimpleCSVReader(@".\MyThirdCSVRecord.csv"))
                {
                    reader.HasHeader = true;
                    MyThirdCSVRecord record;
                    int i = 0;
                    while (serializer.DeserializeLine<MyThirdCSVRecord>(reader, out record))
                    {
                        Assert.AreEqual(_myThirdCSVRecord[i].ColumnA, record.ColumnA);
                        Assert.AreEqual(_myThirdCSVRecord[i].ColumnB, record.ColumnB);
                        Assert.AreEqual(_myThirdCSVRecord[i].ColumnC, record.ColumnC);
                        Assert.AreEqual(_myThirdCSVRecord[i].Total, record.Total);

                        ++i;
                    }


                }
            }

            using (SimpleCSVReader reader = new SimpleCSVReader(@".\MyThirdCSVRecord.csv"))
            {
                int line = 0;
                reader.HasHeader = true;
                while (reader.ReadLine())
                {
                    Assert.IsTrue(String.IsNullOrEmpty(reader[1]));

                    Assert.AreEqual(_myThirdCSVRecord[line].ColumnA, reader[2]);
                    Assert.AreEqual(_myThirdCSVRecord[line].ColumnB, reader[3]);
                    Assert.AreEqual(_myThirdCSVRecord[line].ColumnC, reader[4]);
                    Assert.AreEqual(_myThirdCSVRecord[line].Total.ToString(), reader[5]);

                    // Map by labelled columns
                    Assert.AreEqual(_myThirdCSVRecord[line].ColumnA, reader["A"]);
                    Assert.AreEqual(_myThirdCSVRecord[line].ColumnB, reader["B"]);
                    Assert.AreEqual(_myThirdCSVRecord[line].ColumnC, reader["C"]);
                    Assert.AreEqual(_myThirdCSVRecord[line].Total.ToString(), reader["Total"]);
                    Assert.AreEqual(_myThirdCSVRecord[line].NullabelValue.HasValue ? _myThirdCSVRecord[line].NullabelValue.Value.ToString() : String.Empty, reader["NullabelValue"]);

                    ++line;
                }
            }
        }
Beispiel #4
0
        public void MyFirstCSVRecordWithHeaderSerialization()
        {
            // serialization with no header
            using (SimpleCSVSerializer<MyFirstCSVRecord> serializer = new SimpleCSVSerializer<MyFirstCSVRecord>())
            {
                using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\MyFirstCSVRecordNoHeader.csv"))
                {
                    writer.HasHeader = true;

                    foreach (var record in _myFirstCSVRecord)
                    {
                        serializer.Serialize(writer, record);
                    }
                }

                using (SimpleCSVReader reader = new SimpleCSVReader(@".\MyFirstCSVRecordNoHeader.csv"))
                {
                    reader.HasHeader = true;

                    MyFirstCSVRecord record;
                    int i = 0;
                    while (serializer.DeserializeLine<MyFirstCSVRecord>(reader, out record))
                    {
                        Assert.AreEqual(_myFirstCSVRecord[i].ColumnA, record.ColumnA);
                        Assert.AreEqual(_myFirstCSVRecord[i].ColumnB, record.ColumnB);
                        Assert.AreEqual(_myFirstCSVRecord[i].ColumnC, record.ColumnC);
                        Assert.AreEqual(_myFirstCSVRecord[i].Total, record.Total);

                        ++i;
                    }


                }
            }

            // Reading serialized CSV with standard read
            using (SimpleCSVReader reader = new SimpleCSVReader(@".\MyFirstCSVRecordNoHeader.csv"))
            {
                int line = 0;
                reader.HasHeader = true;

                while (reader.ReadLine())
                {
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnA, reader[1]);
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnB, reader[2]);
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnC, reader[3]);
                    Assert.AreEqual(_myFirstCSVRecord[line].Total.ToString(), reader[4]);

                    // By column labels
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnA, reader["ColumnA"]);
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnB, reader["ColumnB"]);
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnC, reader["ColumnC"]);
                    Assert.AreEqual(_myFirstCSVRecord[line].Total.ToString(), reader["Total"]);

                    ++line;
                }
            }
        }
Beispiel #5
0
        public void MyFirstCSVRecordNoHeaderSerialization()
        {
            // serialization with no header
            using (SimpleCSVSerializer<MyFirstCSVRecord> serializer = new SimpleCSVSerializer<MyFirstCSVRecord>())
            {
                using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\MyFirstCSVRecordNoHeader.csv"))
                {

                    foreach (var record in _myFirstCSVRecord)
                    {
                        serializer.Serialize(writer, record);
                    }
                }

                using (SimpleCSVReader reader = new SimpleCSVReader(@".\MyFirstCSVRecordNoHeader.csv"))
                {
                    MyFirstCSVRecord record;
                    int i = 0;
                    while (serializer.DeserializeLine<MyFirstCSVRecord>(reader, out record))
                    {
                        Assert.AreEqual(_myFirstCSVRecord[i].ColumnA, record.ColumnA);
                        Assert.AreEqual(_myFirstCSVRecord[i].ColumnB, record.ColumnB);
                        Assert.AreEqual(_myFirstCSVRecord[i].ColumnC, record.ColumnC);
                        Assert.AreEqual(_myFirstCSVRecord[i].Total, record.Total);

                        ++i;
                    }


                }
            }

            using (SimpleCSVReader reader = new SimpleCSVReader(@".\MyFirstCSVRecordNoHeader.csv"))
            {
                int line = 0;
                while (reader.ReadLine())
                {
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnA, reader[1]);
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnB, reader[2]);
                    Assert.AreEqual(_myFirstCSVRecord[line].ColumnC, reader[3]);
                    Assert.AreEqual(_myFirstCSVRecord[line].Total.ToString(), reader[4]);

                    ++line;
                }
            }
        }
Beispiel #6
0
        public void BasicReadWriteWithHeader()
        {

            using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\BasicReadWriteWithHeader.csv"))
            {
                writer.WriteHeader(_simpleHeader);

                foreach (var data in _simpleData)
                {
                    writer.WriteLine(data);
                }
            }

            // By manually executing ReadHeader
            using (SimpleCSVReader reader = new SimpleCSVReader(@".\BasicReadWriteWithHeader.csv"))
            {
                int i = 0;

                reader.ReadHeader();

                while (reader.ReadLine())
                {
                    Assert.AreEqual(_simpleData[i].Length, reader.ColumnsCount);
                    Assert.AreEqual(_simpleData[i][0] ?? "", reader[1]);
                    Assert.AreEqual(_simpleData[i][1] ?? "", reader[2]);
                    Assert.AreEqual(_simpleData[i][2] ?? "", reader[3]);

                    ++i;
                }

                Assert.IsTrue(reader.HasHeader);
            }

            // By setting flag HasHeader to true
            using (SimpleCSVReader reader = new SimpleCSVReader(@".\BasicReadWriteWithHeader.csv"))
            {
                int i = 0;

                reader.HasHeader = true;



                while (reader.ReadLine())
                {
                    Assert.AreEqual(_simpleData[i].Length, reader.ColumnsCount);
                    Assert.AreEqual(_simpleData[i][0] ?? "", reader[1]);
                    Assert.AreEqual(_simpleData[i][1] ?? "", reader[2]);
                    Assert.AreEqual(_simpleData[i][2] ?? "", reader[3]);

                    // check searching by header label
                    Assert.AreEqual(_simpleData[i][0] ?? "", reader[_simpleHeader[0]]);
                    Assert.AreEqual(_simpleData[i][1] ?? "", reader[_simpleHeader[1]]);
                    Assert.AreEqual(_simpleData[i][2] ?? "", reader[_simpleHeader[2]]);
                    Assert.IsNull(reader["Some non existing header"]);

                    ++i;
                }

                Assert.IsTrue(reader.HasHeader);
            }
        }
Beispiel #7
0
        public void BasicReadWriteQuoted()
        {

            using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\BasicReadWriteQuoted.csv"))
            {
                writer.QuoteAll = true;

                foreach (var data in _simpleData)
                {
                    writer.WriteLine(data);
                }
            }

            using (SimpleCSVReader reader = new SimpleCSVReader(@".\BasicReadWriteQuoted.csv"))
            {
                int i = 0;
                while (reader.ReadLine())
                {
                    Assert.AreEqual(_simpleData[i].Length, reader.ColumnsCount);
                    Assert.AreEqual(_simpleData[i][0] ?? "", reader[1]);
                    Assert.AreEqual(_simpleData[i][1] ?? "", reader[2]);
                    Assert.AreEqual(_simpleData[i][2] ?? "", reader[3]);

                    ++i;
                }

                Assert.IsFalse(reader.HasHeader);
            }
        }