forked from MarcosMeli/FileHelpers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MarcosMeli#149 Add Feature Padding for delimited records
- Loading branch information
Jimmy
committed
Aug 15, 2018
1 parent
ef80c42
commit 9d328c0
Showing
6 changed files
with
291 additions
and
73 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
FileHelpers.Examples/Examples/18.Converters/70.PaddingConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using FileHelpers; | ||
|
||
namespace ExamplesFx | ||
{ | ||
//-> Name:Write Delimited File | ||
//-> Description:Example of how to write a Delimited File | ||
//-> AutoRun:true | ||
|
||
public class PaddingConverterExample | ||
: ExampleBase | ||
{ | ||
//-> To write an output file (separated by a "|"): | ||
|
||
//-> FileOut: Output.txt | ||
|
||
// -> You use the same Record Mapping Class as you would to read it: | ||
//-> File:RecordClass.cs | ||
/// <summary> | ||
/// Layout for a file delimited by | | ||
/// </summary> | ||
[DelimitedRecord("|")] | ||
public class Orders | ||
{ | ||
public int OrderID; | ||
|
||
[FieldConverter(ConverterKind.Padding, 8, PaddingMode.Left, '0')] | ||
public string CustomerID; | ||
|
||
[FieldConverter(ConverterKind.Date, "ddMMyyyy")] | ||
public DateTime OrderDate; | ||
|
||
[FieldConverter(ConverterKind.Decimal, ".")] // The decimal separator is . | ||
public decimal Freight; | ||
} | ||
|
||
//-> /File | ||
|
||
//-> Instantiate a FileHelperEngine and write the file: | ||
|
||
public override void Run() | ||
{ | ||
//-> File:Example.cs | ||
var engine = new FileHelperEngine<Orders>(); | ||
|
||
var orders = new List<Orders>(); | ||
|
||
orders.Add(new Orders() | ||
{ | ||
OrderID = 1, | ||
CustomerID = "9001", | ||
Freight = 82.43M, | ||
OrderDate = new DateTime(2009, 05, 01) | ||
}); | ||
|
||
orders.Add(new Orders() | ||
{ | ||
OrderID = 2, | ||
CustomerID = "9002", | ||
Freight = 12.22M, | ||
OrderDate = new DateTime(2009, 05, 02) | ||
}); | ||
|
||
//engine now supports custom EOL for writing | ||
engine.NewLineForWrite = "\n"; | ||
|
||
engine.WriteFile("Output.Txt", orders); | ||
|
||
//-> /File | ||
|
||
Console.WriteLine(engine.WriteString(orders)); | ||
} | ||
|
||
//-> The classes you use could come from anywhere: LINQ to Entities, SQL database reads, or in this case, classes created within an application. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.