Paul Klink

A little bit about me

Paul Klink

Paul Klink

A little bit about me

Writing of Declared Fielded Text file

This examples shows how a Declared Fielded Text file can be written. It uses the Basic Example Meta File.

Declared Fielded Text File

The Declared Fielded Text File to be written is:

!|!Fielded Text^| Version="1.1"
! MetaEmbedded="True"
! <?xml version="1.0" encoding="utf-8"?>
! <FieldedText LineCommentChar="!">
!   <Field Name="PetName" />
!   <Field Name="Age" DataType="Float" />
!   <Field Name="Color" />
!   <Field Name="DateReceived" DataType="DateTime" Format="d MMM yyyy" />
!   <Field Name="Price" DataType="Decimal" />
!   <Field Name="NeedsWalking" DataType="Boolean" />
!   <Field Name="Type" />
! </FieldedText>
Rover,4.5,Brown,12 Feb 2004,80,True,Dog
Charlie,,Gold,5 Apr 2007,12.3,False,Fish

Console Application

using System;
using Xilytix.FieldedText;

namespace Write_Declared
{
    class Program
    {
        // Example of using FtWriter to write a declared Fielded Text
        // (CSV) file.
        static void Main(string[] args)
        {
            // Name of file containing Meta
            const string MetaFileName = "BasicExampleMeta.ftm";
            // Name of file to be written
            const string FtxFileName = "BasicExample.ftx";

            // Define FieldNames
            const string PetNameFieldName = "PetName";
            const string AgeFieldName = "Age";
            const string ColorFieldName = "Color";
            const string DateReceivedFieldName = "DateReceived";
            const string PriceFieldName = "Price";
            const string NeedsWalkingFieldName = "NeedsWalking";
            const string TypeFieldName = "Type";

            // Create Meta from file
            FtMeta meta = FtMetaSerializer.Deserialize(MetaFileName);
            meta.LineCommentChar = '!'; // Is not set.  Change to !
            meta.HeadingLineCount = 0; // Change to 0. Do not want any
                                       // heading lines in this example

            // Create FtWriterSettings to flag we want Declared file
            // written
            FtWriterSettings settings = new FtWriterSettings();
            settings.Declared = true;
            settings.MetaReferenceType = FtMetaReferenceType.Embedded;

            // Create Writer
            using (FtWriter writer = new FtWriter(meta, FtxFileName, settings))
            {
                // Write 1st Record
                writer[PetNameFieldName] = "Rover";
                writer[AgeFieldName] = 4.5;
                writer[ColorFieldName] = "Brown";
                writer[DateReceivedFieldName] = new DateTime(2004, 2, 12);
                writer[PriceFieldName] = 80M;
                writer[NeedsWalkingFieldName] = true;
                writer[TypeFieldName] = "Dog";

                writer.Write();

                // Write 2nd Record
                writer[PetNameFieldName] = "Charlie";
                writer[AgeFieldName] = null;
                writer[ColorFieldName] = "Gold";
                writer[DateReceivedFieldName] = new DateTime(2007, 4, 5);
                writer[PriceFieldName] = 12.3M;
                writer[NeedsWalkingFieldName] = false;
                writer[TypeFieldName] = "Fish";

                writer.Write();
            }
        }
    }
}