0

How do I go about removing the optional field from the text field that I have output using the Filehelpers library. I'm using c#

For example, I have a shared class file with attributes such as recordnumber, filler, payment, endingspaces

Then I need to write only recordnumber and payment into the text file without the filler.

[FixedLengthRecord(FixedMode.ExactLength)]
public partial class Person
{
[FieldFixedLength(10)]
public string FirstName;
[FieldFixedLength(10)]
public string LastName;

[FieldOptional]
[FieldFixedLength(5)]
public string Optional1;

[FieldOptional]
[FieldFixedLength(5)]
public string Optional2;

[FieldOptional]
[FieldFixedLength(5)]
public string Optional3;

}

class Program
{
private static void Main(string[] args)
 {
  var engine = new FileHelperEngine<Person>();
  Person[] allPersonRecords = GetPersonExportFromDataBase() as Person[];//This will only get the FirstName,LastName,Optional2. No result for Optional1 and Optional3

  FileHelperEngine enginePerson = new FileHelperEngine(typeof(Person));

  enginePerson.AppendToFile(FileName, allPersonRecords ); //Write the records to the file

//Current Output looks like this:John      Lee            title     
//The output which I want is:John      Lee       title

 }
}

2 Answers2

0

You can use the INotifyWrite attribute to intercept the line before it is written and modify it. Here is a working example.

[DelimitedRecord(",")]
public partial class Person : INotifyWrite<Person>
{
    public string FirstName;
    public string LastName;
    [FieldOptional]
    public string Optional1;
    [FieldOptional]
    public string Optional2;
    [FieldOptional]
    public string Optional3;

    public void BeforeWrite(BeforeWriteEventArgs<Person> e)
    {
    }

    public void AfterWrite(AfterWriteEventArgs<Person> e)
    {
        // count the non-optional fields
        var numberOfNonOptionalFields = typeof(Person).GetFields()
            .Where(f => !f.GetCustomAttributes(false).Any(a => a is FieldOptionalAttribute))
            .Count();

        // take only the first n tokens
        e.RecordLine = String.Join(",", e.RecordLine.Split(',').Take(numberOfNonOptionalFields));
    }
}      

class Program
{
    private static void Main(string[] args)
    {
        var engine = new FileHelperEngine<Person>();
        var export = engine.WriteString(
                     new Person[] { 
                       new Person() { 
                           FirstName = "Joe", 
                           LastName = "Bloggs", 
                           Optional1 = "Option 1", 
                           Optional2 = "Option 2", 
                           Optional3 = "Option 3" 
                       } 
                     });
        Assert.AreEqual("Joe,Bloggs" + Environment.NewLine, export);
        Console.WriteLine("Export was as expected");
        Console.ReadLine();
    }
}
shamp00
  • 11,106
  • 4
  • 38
  • 81
  • Thanks for the reply, I have tried your codes but I'm not able to perform what I require. Sorry, able to advise me a bit further based on my above codes? – namelessly12 Mar 04 '15 at 04:15
  • Then modify `AfterWrite()` to take the just the parts you need: `e.RecordLine = e.RecordLine.Substring(0,20) + e.RecordLine.Substring(25,5)` – shamp00 Mar 04 '15 at 10:12
  • Don't forget `Person` needs to implement `INotifyWrite` otherwise the `AfterWrite` will never get called. – shamp00 Mar 04 '15 at 10:13
0

If you want to just statically omit the optional fields (i.e. they are never used and you never want to output them) you could just create another class representing the desired output format and then convert the list from one object type to another using LINQ:

class Program
{
    static void Main(string[] args)
    {
        // dummy test data
        var originalAllPersonRecords = new Person[]
        {
            new Person { FirstName = "John", LastName = "Lee", Optional2 = "title" },
        };//This will only get the FirstName,LastName,Optional2. No result for Optional1 and Optional3

        var allPersonRecords = from p in originalAllPersonRecords select new OutputPerson{ FirstName = p.FirstName, LastName = p.LastName, Optional2 = p.Optional2 };

        FileHelperEngine enginePerson = new FileHelperEngine(typeof(OutputPerson));

        string fileName = "Wherever you want the file to go";
        enginePerson.AppendToFile(fileName, allPersonRecords); //Write the records to the file

        //Current Output looks like this:John      Lee            title     
        //The output which I want is:John      Lee       title
    }
}
//New class added representing the output format
[FixedLengthRecord(FixedMode.ExactLength)]
class OutputPerson
{
    [FieldFixedLength(10)]
    public string FirstName;

    [FieldFixedLength(10)]
    public string LastName;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional2;
}

[FixedLengthRecord(FixedMode.ExactLength)]
class Person
{
    [FieldFixedLength(10)]
    public string FirstName;
    [FieldFixedLength(10)]
    public string LastName;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional1;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional2;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional3;
}
Ceisc
  • 1,278
  • 12
  • 18