FRENDS Technology logo

Message Processing HowTo - Transforming flat file to XML file

This sample shows how you can enable custom file transformation in Cobalt. In this sample case source file is converted from a .csv file to XML file.

Implement needed operations

NOTE: For the location of source files see extending cobalt.

For custom file transformations you have to create a custom message processing step derived from MessageProcessingStepBase abstract base class.

01public class FlatFileToXmlTransformer : MessageProcessingStepBase
02{
03    public FlatFileToXmlTransformer(MessageProcessingStepConfig parameters): base(parameters){}
04 
05    public override FileInfo Execute(FileInfo inputFile, ExtendedTransferContext context)
06    {
07        FileInfo outputFile = context.GetNewWorkFileName();
08        TransformInputFileToXml(inputFile, outputFile);
09        return outputFile;
10    }
11 
12    public void TransformInputFileToXml(FileInfo inputFile, FileInfo outputFile)
13    {
14        FileHelperEngine engine = new FileHelperEngine(typeof(Invoice));
15 
16        InvoiceList recordList = new InvoiceList();
17        recordList.Invoices = (Invoice[])engine.ReadFile(inputFile.FullName);
18 
19        XmlSerializer serializer = new XmlSerializer(typeof(InvoiceList));
20 
21        using (TextWriter writer = new StreamWriter(outputFile.FullName))
22            serializer.Serialize(writer, recordList);
23    }
24 
25    public override void Dispose(){}
26}

Constructor with MessageProcessingStepConfig as constructor parameter is required for derived class MessageProcessingStepBase but since we don't really need the parameters for anything in this case, we can leave the constructor empty.

For parsing the source file we use FileHelpers library. For delimited record files we need to create a class for mapping the source file. Delimiter for source file is provided with DelimitedRecord attribute. Class itself contains fields to which to map the source file fields. No methods are needed to be written.

1[DelimitedRecord(";")]
2public class Invoice
3{
4    public string CustomerName;
5    public string Address;
6    public string Sum;
7    [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
8    public DateTime DueDate;
9}       

Parsing the source file is done by first generating an instance of FileHelperEngine with Invoice given to constructor's Type parameter. File is read by calling ReadFile() method, which takes filepath as its parameter.

1FileHelperEngine engine = new FileHelperEngine(typeof(Invoice));
2 
3InvoiceList recordList = new InvoiceList();
4recordList.Invoices = (Invoice[])engine.ReadFile(inputFile);       

For more information about FileHelpers, see FileHelpers Library home page.

In this sample the XML structure is created with XML Serialization. The root element is InvoiceList which has Invoice child elements which are serialized from an Invoice array.

1[XmlRoot("InvoiceList")]
2public class InvoiceList
3{
4    [XmlElement("Invoice")]
5    public Invoice[] Invoices { get; set; }
6}       
1XmlSerializer serializer = new XmlSerializer(typeof(InvoiceList));
2 
3using (TextWriter writer = new StreamWriter(outputFile))
4    serializer.Serialize(writer, recordList);

Step 1: Compile and deploy the assembly

In order to use the code with Cobalt you first need to compile it to an assembly and deploy the assembly somewhere FRENDS Iron can find the code. This means either the \Program Files\Frends Technology\FRENDS Iron -directory or the GAC

Step 2: Configure Cobalt

Once the assembly is deployed, you need to configure Cobalt to use the new custom MessageProcessing step. To do this, just create a new Cobalt connection point as described in the manual and:

Custom Message Processing: Flat to XML conversion

Step 3: Create file to transform

In this example the sample file contains simple invoicing data. Each row contains 4 fields delimited with a semicolon.

Create a file with the following contents in Cobalt input directory. Name the file to match your Cobalt Source FileName pattern.

John Doe;Main Street 12 A 5;43.35;08-12-2009
Jane Doe;Hill Street 124;59.32;15-12-2009

Note that when we use FileHelper to parse the source file, there must not be a semicolon after the last field as it will fail the mapping between the source file and the mapping class we created earlier. If lines have ending semicolon it can be worked around with adding an extra dummy field at the end of the Invoice class. Notice that the dummy field is also included in the XML file if XML Serialization is used.

Step 4: Create and execute the file transfer routine

As in the manual create and execute file transfer routine.