Esempio n. 1
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);
            }
        }
Esempio n. 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();
        }
Esempio n. 3
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);
            }
        }