Is there a way to do field order using the FileHelpers library?
Asked Answered
A

1

8

I downloaded FileHelpers from nuget but I am not sure if this feature does not exist or if I don't have the right version or what.

I been looking around and it seems that FileHelpers may have an attribute to specify the field order.

I downloaded this one however when I was looking in nuget there seems to be another version

Annunciate answered 11/1, 2012 at 23:18 Comment(9)
Based on the code in the bitbucket repository it looks like the FieldOrderAttribute has been in there since 03/02/2010 which is much older than the package you have installed.Compiler
Also the more active SourceForge repository shows the file has existed since at least March 2010.Compiler
So the nuget ones are way out of date then?Annunciate
The nuget package you installed is dated Jan 2011 so I would guess they include the functionality. If it isn't in there then I would bet so.Compiler
HI there, the problem with NuGet is that we (The FileHelpers developers) don't create the package, I don't know who do it, so we can add the last beta versions :( You can download the last stable build from here: teamcity.codebetter.com/… CheersConverse
@MarcosMeli - well since your on the team can't you guys make an offical nuget package? I mean there is already 2 made might as well call make it 3 and have yours as the official one. Or get there removed and only have yours. It's not good that these people have unofficial ones that never get updated as it can confuse user and if they are like me and try to use nuget for everything they might not notice until they start looking for features and find they do exist.Annunciate
@Annunciate You are right, I want to create the package for version 3.0 after vacations in a month or so, we will ask the nuget admins to remove the others or give us admin control of themConverse
Cool. Can't wait to see what 3.0 brings to the table. Can you update this post when it comes out?Annunciate
@MarcosMeli - how can I submit a patch for Filehelpers? I tried via email and sourceforge trac. Not required, but if you could move (or sync) to github, I can then just submit pull requests. :)Trigg
J
9

Firstly, the FieldOrder attribute does not exist in FileHelpers 2.0. In FileHelpers 2.9.9 (also available via NuGet), the attribute exists but if you specify it for any field, you must specify it for all fields. In general, however, use of the attribute is no necessary, since the order of the fields is defined by the format.

When using FileHelpers you provide a class to describe your format, e.g.,

[DelimitedRecord("|")] 
public class Order 
{ 
   // First field
   public int OrderID; 

   // Second field
   public string CustomerID; 

   // Third field
   [FieldConverter(ConverterKind.Date, "ddMMyyyy")]   
   public DateTime OrderDate;    
}

This describes a format with three fields, separated by vertical bars. If you like, it is the specification of the format. Once defined you can use it to import and export:

FileHelperEngine engine = new FileHelperEngine(typeof(Order)); 

// To read use: 
Order[] orders = engine.ReadFile("FileIn.txt") as Order[]; 

// To write use: 
engine.WriteFile("FileOut.txt", orders); 

So, if you want your fields in a different order, you should modify your Order class.

Now if you really wanted to, (with FileHelpers 2.9.9), you could change the order of the fields as follows:

[DelimitedRecord("|")] 
public class Order 
{ 
   // Third field
   [FieldOrder(3)]
   public int OrderID; 

   // Second field
   [FieldOrder(2)]
   public string CustomerID; 

   // First field
   [FieldOrder(1)]
   [FieldConverter(ConverterKind.Date, "ddMMyyyy")]   
   public DateTime OrderDate;    
}

but it is cleaner to avoid the use of the FieldOrder attribute and modify the order of the fields within the class instead.

On the other hand, if you need to specify the field order at runtime, you should build the Order class at using runtime records. You can use a string

Type orderType = ClassBuilder.ClassFromString(stringContainingOrderClassInCSharp); 

FileHelperEngine engine = new FileHelperEngine(orderType); 
Order[] orders = engine.ReadFile("FileIn.txt") as Order[]; 

Or you can use a ClassBuilder:

DelimitedClassBuilder cb = new DelimitedClassBuilder("Order");
// First field
cb.AddField("OrderID", typeof(int));
// Second field
cb.AddField("CustomerID", 8, typeof(string));
// Third field
cb.AddField("OrderDate", typeof(DateTime));
cb.LastField.Converter.Kind = ConverterKind.Date; 
cb.LastField.Converter.Arg1 = "ddMMyyyy";

engine = new FileHelperEngine(cb.CreateRecordClass());
Order[] orders = engine.ReadFile("FileIn.txt") as Order[]; 

You can use whatever logic you like in order to add your fields in the necessary order.

Jareb answered 12/1, 2012 at 10:13 Comment(9)
What is the difference? FileHelpers 2.9.9 - This version seems to be missing this attribute "FieldTitle" (what was in one of the links I linked too)Annunciate
FileHelpers 2.0.0.0 was developed between 2005 and 2007 and is stable and has lots of documentation. The more recent version 2.9.9 has less documentation and I'm not sure what the new features are. FieldTitle has been replaced with an engine.HeaderText property which you can set as explained in the answer to your linked postJareb
I was also looking at your code and realized your not using properties. Can you not use properties. I see that automatic properties do not work with FieldOrderAnnunciate
No FileHelpers classes (in version 2.0 at least) do not work with properties. They use public fields instead.Jareb
can I use property with a backing? Also I am not sure if this will have to be a new question but I am having problems with my header row. "Error Converting '"Date"(my propertyName)' to type: 'DateTime'. There are less chars in the Input String than in the Format string: 'mmddyyyy'"Annunciate
No you cannot use properties in the FileHelpers classes. (The FileHelpers classes are for describing a format, not for any logic you might want to put in a getter or setter.) If you need properties, you should enumerate the objects returned by engine.ReadFile and copy the imported fields to some other class's properties. Are you trying to import or export? If you are importing you should not need to worry about the header - just use [IgnoreFirst(1)] to skip the first line altogether.Jareb
I always make properties(hence why they got automatic properties) even if there is no logic as I thought that was best practice). It seems that fully backed properties do work though. However [IgnoreFirst(1)] does not seem to work(even if I use field variables). It does not want to skip the first record.Annunciate
let us continue this discussion in chatJareb
@Jareb Old post, but your ClassBuilder example casts run time class "Order" to compile time class Order. They are different classes and as Order[] will always return null. I'm not aware of any straight forward way of getting back compile time class rows (which would be ideal to do generics etc.), although there is an FH issue on this.Meso

© 2022 - 2024 — McMap. All rights reserved.