Home Introduction Capabilities News Software Examples FAQ Standard Licence Contact

Fielded Text:

Xilytix: Read a Basic CSV File (C#)

"Fielded Text" trademark owned by Paul Klink

Step 9: Compile and Run

Compile and run the program.  When the button is clicked, the program will do the following:

  1. Create a TFieldedText object
  2. Load the Meta
  3. Parse the file once to get headings
  4. Create DataTables in which to store headings and records
  5. Create columns for the DataTables using the Fields property in the FieldedText object
  6. Read the Headings from the TFieldedText object into the Headings DataTable
  7. Parse the file again with an IDataReader to get Records
  8. Place each record into a row in the Records DataTable
  9. Assign the DataTables to the Grids on the Form

"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, 12 Dec 2006, 25, False, "Cat"

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

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Text;

using System.IO;

using Xilytix.FieldedText;

{

  TFieldedText FieldedText = new TFieldedText();


  // Load the Meta which describes the text file

  // A Fielded Text Editor is used to create the Meta File.

  FieldedText.LoadMeta("BasicExample.ftm");


  // Parse the file once to load headings

  FieldedText.Parse("BasicReadExample.txt", true);


  // Create DataTables in which to store parsed data

  DataTable RecordsTable = new DataTable("Records");

  DataTable HeadingsTable = new DataTable("Headings");


  // Create Columns

  for (int FieldIdx = 0; FieldIdx < FieldedText.FieldCount; FieldIdx++)

  {

    TFieldedTextField Field = FieldedText.GetFields(FieldIdx);

    RecordsTable.Columns.Add(Field.Name, Field.ValueType);

    HeadingsTable.Columns.Add(Field.Name, System.Type.GetType("System.String"));

  }


  // Read Headings and add as rows to table

  for (int LineIdx = 0; LineIdx < FieldedText.HeadingLineCount; LineIdx++)

  {

    DataRow Row = HeadingsTable.NewRow();

    for (int FieldIdx = 0; FieldIdx < FieldedText.FieldCount; FieldIdx++)

    {

      TFieldedTextField Field = FieldedText.GetFields(FieldIdx);

      Row[Field.Name] = Field.GetHeadings(LineIdx);

    }


    HeadingsTable.Rows.Add(Row);

  }


  // Use an IDataReader to get data for records

  IDataReader Reader = FieldedText.ExecuteReader("BasicReadExample.txt");


  // Use GetOrdinal to reference fields

  int PetNameOrd = Reader.GetOrdinal("PetName");

  int AgeOrd = Reader.GetOrdinal("Age");

  int ColorOrd = Reader.GetOrdinal("Color");

  int DateReceivedOrd = Reader.GetOrdinal("DateReceived");

  int PriceOrd = Reader.GetOrdinal("Price");

  int NeedsWalkingOrd = Reader.GetOrdinal("NeedsWalking");

  int TypeOrd = Reader.GetOrdinal("Type");


  // Read records and add as rows to table

  while (Reader.Read())

  {

    DataRow Row = RecordsTable.NewRow();

    Row[Reader.GetName(PetNameOrd)] = Reader.GetValue(PetNameOrd);

    if (Reader.IsDBNull(AgeOrd))

      Row[Reader.GetName(AgeOrd)] = DBNull.Value;

    else

      Row[Reader.GetName(AgeOrd)] = Reader.GetValue(AgeOrd);

    Row[Reader.GetName(ColorOrd)] = Reader.GetValue(ColorOrd);

    Row[Reader.GetName(DateReceivedOrd)] = Reader.GetValue(DateReceivedOrd);

    Row[Reader.GetName(PriceOrd)] = Reader.GetValue(PriceOrd);

    Row[Reader.GetName(NeedsWalkingOrd)] = Reader.GetValue(NeedsWalkingOrd);

    Row[Reader.GetName(TypeOrd)] = Reader.GetValue(TypeOrd);


    RecordsTable.Rows.Add(Row);

  }


  HeadingsGrid.DataSource = HeadingsTable;

  RecordsGrid.DataSource = RecordsTable;

}

This tutorial demonstrates how to use the Xilytix TFieldedText component to read a CSV file and place the headings and records into a .NET WinForms DataGrid.

The contents of the CSV file to be imported are shown here


This text file has 2 headings and 4 records.  This tutorial will show how to read this file into WinForm Form which has 2 DataGrids; one for the headings and the other for the records.  The full source code for this tutorial lesson can be downloaded from the component's home page

Step 1: Select a folder

Create or select an empty folder for the program we are going to develop.

Step 2: Get text file

Create a text file called "BasicReadExample.txt" and place the contents of the above CSV file into it.  Save this file to the folder where the compiled executable will run from.  Alternatively download the text file from here (Right Click | Save Link/Target As) and save to the folder where the compiled executable will run from.

Step 3: Create Fielded Text Meta file

The Meta file specifies the structure of the text file.  In this step we will create a Meta file called "BasicExample.ftm" for the text file.  There are 2 ways of doing this.

The easy way is to simply download the Meta file from here (Right Click | Save Link/Target As) and place in the folder where the compiled executable will run from.

Alternatively, you can create it with a Fielded Text Editor.  This will provide a better understanding of what a Meta file actually is.  Follow the steps here to create the "BasicExample.ftm" and then save it into the folder where the compiled executable will run from.

The contents of the Fielded Text Meta File should be similar to the text shown below

?xml version="1.0" encoding="utf-8"?>

<FieldedText HeadingLineCount="2" HeadingWritePrefixSpace="True">

  <Field Name="PetName" ValueQuotedType="Always" HeadingWritePrefixSpace="False" Headings="Pet Name" />

  <Field DataType="Float" Name="Age" ValueWritePrefixSpace="True" Headings="Age, " />

  <Field Name="Color" ValueWritePrefixSpace="True" Headings="Color, (Years)" />

  <Field DataType="DateTime" Name="DateReceived" ValueWritePrefixSpace="True" Headings="Date Received, " Format="d MMM yyyy" />

  <Field DataType="Decimal" Name="Price" ValueWritePrefixSpace="True" Headings="Price, (Dollars)" />

  <Field DataType="Boolean" Name="NeedsWalking" ValueWritePrefixSpace="True" Headings="Needs Walking, " />

  <Field Name="Type" ValueQuotedType="Always" ValueWritePrefixSpace="True" Headings="Type" />

</FieldedText>

Step 4: Create a WinForm project

Create a WinForm project in your development environment.

Step 5: Reference the FieldedText Assembly

Add a reference to the Xilytix.FieldedText.dll and Borland.Delphi.dll assemblies.  These assemblies can be downloaded from here.

Step 6: Place controls on Form

Add the following items to a form in the project:

Step 7: Add 'using' lines

Make sure that the following 'using' lines are included in the code for the form.



Step 8: Button Click event

Add the following code to the Click event of the button: