Getting Started with EDIFACTTranslator

Requirements:
EDIFACT Integrator

Introduction

The EDIFACT Integrator includes components for reading, writing, and translating EDIFACT documents. This article will focus on using the EDIFACTTranslator component to translate from EDI to XML and also from XML to EDI.

Contents

  1. Converting EDI to XML
  2. Converting XML to EDI

Converting EDI to XML

The EDIFACTTranslator component is designed to convert EDIFACT documents to XML and vice versa. The steps below show a typical process for converting from EDIFACT to XML. Please consult the help documentation for additional options and details.

Quick Example

The following sections discuss the details of how EDI documents can be translated to XML. The following sample code will be used as a basis for discussing these options.

EDIFACTTranslator1.SchemaFormat = EdifacttranslatorSchemaFormats.schemaJSON;
EDIFACTTranslator1.LoadSchema("C:\\schemas\\RSSBus_D09B_ORDERS.json");
EDIFACTTranslator1.InputFile = myEDIFile;

//EDI -> XML
EDIFACTTranslator1.Translate();
string translatedXML = EDIFACTTranslator1.OutputData;

//XML -> EDI
EDIFACTTranslator1.Reset();
EDIFACTTranslator1.InputFormat = EdifacttranslatorInputFormats.ifXML;
EDIFACTTranslator1.InputData = translatedXML;
EDIFACTTranslator1.Translate();

string translatedEDI = EDIFACTTranslator1.OutputData;

Loading an EDI Schema

To begin, it is recommended to provide an EDI schema describe the EDIFACT document that will be translated. This is not required, but it does allow details such as proper element names and descriptive details to be included in the translated XML.

If you do not already have a set of schemas it is recommended to use the RSSBus JSON schema (http://www.rssbus.com/schemas/).

The component supports the following schema formats:

In this article we will use a D09B ORDERS message with a Altova schema. To load the schema call the LoadSchema method. For instance:

EDIFACTTranslator1.SchemaFormat = EdifacttranslatorSchemaFormats.schemaJSON;
EDIFACTTranslator1.LoadSchema("C:\\schemas\\RSSBus_D09B_ORDERS.json");

Renaming Rules

The EDIFACTTranslator component optionally supports renaming EDI elements when translating to XML. By default the XML element names will be automatically determined by the component from the EDIFACT document and schema. For instance:

EDIFACT data

UNA:+.? '
UNB+UNOA:2+MGATE1:65+TESTER:65+020508:1413+0709210008' 
UNH+1+ORDERS:D:09B:UN:1.1'
BGM+E40+MKIDI5422'
DTM+137:199904081315:203'
DTM+203:20110408:102'
DTM+Z02:20110408:203'
DTM+273:201011:610'
...

Translated to EDI without renaming:

<Interchange>
<Meta>
<UNB1 type="Composite"><Component>UNOA</Component><Component>2</Component></UNB1>
<UNB2 type="Composite"><Component>MGATE1</Component><Component>65</Component></UNB2>
<UNB3 type="Composite"><Component>TESTER</Component><Component>65</Component></UNB3>
<UNB4 type="Composite"><Component>020508</Component><Component>1413</Component></UNB4>
<UNB5>0709210008</UNB5>
</Meta>
<TransactionSet>
<Meta>
<F0062 desc="MESSAGE REFERENCE NUMBER">1</F0062>
<S009 type="Composite" desc="MESSAGE IDENTIFIER"><Component>ORDERS</Component><Component>D</Component><Component>09B</Component><Component>UN</Component><Component>1.1</Component></S009>
</Meta>
<BGM type="Segment" desc="BEGINNING OF MESSAGE">
<C002 type="Composite" desc="DOCUMENT/MESSAGE NAME"><Component>E40</Component></C002>
<C106 type="Composite" desc="DOCUMENT/MESSAGE IDENTIFICATION"><Component>MKIDI5422</Component></C106>
</BGM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<C507 type="Composite" desc="DATE/TIME/PERIOD"><Component>137</Component><Component>199904081315</Component><Component>203</Component></C507>
</DTM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<C507 type="Composite" desc="DATE/TIME/PERIOD"><Component>203</Component><Component>20110408</Component><Component>102</Component></C507>
</DTM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<C507 type="Composite" desc="DATE/TIME/PERIOD"><Component>Z02</Component><Component>20110408</Component><Component>203</Component></C507>
</DTM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<C507 type="Composite" desc="DATE/TIME/PERIOD"><Component>273</Component><Component>201011</Component><Component>610</Component></C507>
</DTM>
...

For convenience of the entity consuming the XML data the EDI elements can be renamed when converting to XML. Renaming rules take the format EDIName:XMLName. For instance

//Define renaming rules
EDIFACTTranslator1.AddRenamingRule("F0062:MessageRefNum);
EDIFACTTranslator1.AddRenamingRule("C507:DateTimePeriod");

The above code will result in XML that is translated with renaming.

lang='csharp'>
<Interchange>
<Meta>
<UNB1 type="Composite"><Component>UNOA</Component><Component>2</Component></UNB1>
<UNB2 type="Composite"><Component>MGATE1</Component><Component>65</Component></UNB2>
<UNB3 type="Composite"><Component>TESTER</Component><Component>65</Component></UNB3>
<UNB4 type="Composite"><Component>020508</Component><Component>1413</Component></UNB4>
<UNB5>0709210008</UNB5>
</Meta>
<TransactionSet>
<Meta>
<MessageRefNum desc="MESSAGE REFERENCE NUMBER">1</MessageRefNum>
<S009 type="Composite" desc="MESSAGE IDENTIFIER"><Component>ORDERS</Component><Component>D</Component><Component>09B</Component><Component>UN</Component><Component>1.1</Component></S009>
</Meta>
<BGM type="Segment" desc="BEGINNING OF MESSAGE">
<C002 type="Composite" desc="DOCUMENT/MESSAGE NAME"><Component>E40</Component></C002>
<C106 type="Composite" desc="DOCUMENT/MESSAGE IDENTIFICATION"><Component>MKIDI5422</Component></C106>
</BGM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<DateTimePeriod type="Composite" desc="DATE/TIME/PERIOD"><Component>137</Component><Component>199904081315</Component><Component>203</Component></DateTimePeriod>
</DTM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<DateTimePeriod type="Composite" desc="DATE/TIME/PERIOD"><Component>203</Component><Component>20110408</Component><Component>102</Component></DateTimePeriod>
</DTM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<DateTimePeriod type="Composite" desc="DATE/TIME/PERIOD"><Component>Z02</Component><Component>20110408</Component><Component>203</Component></DateTimePeriod>
</DTM>
<DTM type="Segment" desc="DATE/TIME/PERIOD">
<DateTimePeriod type="Composite" desc="DATE/TIME/PERIOD"><Component>273</Component><Component>201011</Component><Component>610</Component></DateTimePeriod>
</DTM>

As seen above the XML now contains friendly element names as defined by the renaming rule. To highlight this:

No Renaming:
<F0062 desc="MESSAGE REFERENCE NUMBER">1</F0062>

Renaming rule "F0062:MessageRefNum":
<MessageRefNum desc="MESSAGE REFERENCE NUMBER">1</MessageRefNum>

Renaming rules can also be saved and loaded by calling SaveRenamingRules and LoadRenamingRules.

Translating to XML

Translating from EDI to XML is as simple as calling the Translate method. EDI data can be read from a string, a file on disk, or a stream. Likewise XML data can be saved to a string, a file on disk, or a stream. Please refer to the Quick Sample section for a code example.

After translating the EDI data to XML you may optionally call ExportXMLSchema. This method outputs a XML schema (.xsd) which can be helpful when constructing XML messages outside of the component which can later be translated back to EDI.

Converting XML to EDI

To translate XML to EDI specify the XML input, set InputFormat to indicate that the input is XML, and call the Translate method. The EDI schema is not required.

The InputFormat property must be set to EdifacttranslatorInputFormats.ifXML. This value tells the component to translate from XML to EDI instead of the other way around. The output format is always assumed based on the value set in InputFormat.

//XML -> EDI
EDIFACTTranslator1.Reset();
EDIFACTTranslator1.InputFormat = EdifacttranslatorInputFormats.ifXML;
EDIFACTTranslator1.InputData = translatedXML;

EDIFACTTranslator1.Translate();

string translatedEDI = EDIFACTTranslator1.OutputData;


We appreciate your feedback.  If you have any questions, comments, or suggestions about this article please contact our support team at kb@nsoftware.com.

 
 
Downloads