Home Products Fielded Text About

Xilytix: Reading of CSV file using events

This examples shows how a CSV file can be read using events.  An event is fired when each heading or field value is parsed.  Events are also fired when a heading line and record are started and finished.  This example uses the Basic Example Meta File.

CSV File:

"Pet Name","Age","Color","Date Received","Price","Needs Walking","Type"

"","(Years)","","","(Dollars)","",""

Rover,4.5,Brown,12 Feb 2004,80,True,Dog

Charlie,,Gold,5 Apr 2007,12.3,False,Fish

Molly,2,Black,25 Dec 2006,25,False,Cat

Gilly,,White,10 Apr 2007,10,False,Guinea Pig

Console Application consists of 2 files: Program.cs and Session.cs

using System;

using Xilytix.FieldedText;


namespace Read_Events

{

    public class Session

    {

        // Name of file containing Meta

        const string MetaFileName = "BasicExampleMeta.ftm";

        // Name of file to be read

        const string CsvFileName = "BasicExample.csv";


        

        string[] headings = new string[7]; // Buffer for headings in a heading line

        object[] recObjects = new object[7]; // Buffer for values in a record


        public void Main()

        {

            // Create Meta from file

            FtMeta meta = FtMetaSerializer.Deserialize(MetaFileName);


            // Create Reader

            // Do not read header immediately otherwise heading events will not fire

            using (FtReader reader = new FtReader(meta, CsvFileName, false))

            {

                reader.FieldHeadingReadReady += HandleFieldHeadingReadReady;

                reader.FieldValueReadReady += HandleFieldValueReadReady;

                reader.HeadingLineStarted += HandleHeadingLineStarted;

                reader.HeadingLineFinished += HandleHeadingLineFinished;

                reader.RecordStarted += HandleRecordStarted;

                reader.RecordFinished += HandleRecordFinished;


                // Read all header and then all records.

                // Headings and values will be obtained via the events

                reader.ReadToEnd();

            }

        }


        private void HandleFieldHeadingReadReady(object sender, FtFieldHeadingReadyEventArgs e)

        {

            headings[e.Field.Index] = e.Field.Headings[e.LineIndex];

        }


        private void HandleFieldValueReadReady(object sender, FtFieldValueReadyEventArgs e)

        {

            recObjects[e.Field.Index] = e.Field.AsObject;

        }


        private void HandleHeadingLineStarted(object sender, FtHeadingLineStartedEventArgs e)

        {

            for (int i = 0; i < headings.Length; i++)

            {

                headings[i] = null;

            }

        }


        private void HandleHeadingLineFinished(object sender, FtHeadingLineFinishedEventArgs e)

        {

            Console.WriteLine("Heading " + e.LineIndex.ToString() + ": " +

                              string.Join(",", headings));

        }


        private void HandleRecordStarted(object sender, FtRecordStartedEventArgs e)

        {

            for (int i = 0; i < recObjects.Length; i++)

            {

                recObjects[i] = null;

            }

        }


        private void HandleRecordFinished(object sender, FtRecordFinishedEventArgs e)

        {

            Console.WriteLine("Record " + e.RecordIndex.ToString() + ": " +

                              string.Join(",", recObjects));

        }

    }

}

namespace Read_Events

{

    class Program

    {

        static void Main(string[] args)

        {

            // Need an object for event handlers.

            // Continue example in session class

            Session session = new Session();

            session.Main();

        }

    }

}