Example Applications
This section presents two simple applications: one showing the conversion of an EDI file to XML, and another that shows how to use the API to create an XML Schema from an EDI file.
See XML Converters™ Examples for other application examples.
Converting EDI to XML
Following is a simple example application that reads EDI from one file (
myEdi.x12
) and writes XML to another (myEdi.x12.xml
).using System; using System.Collections.Generic; using System.Text; using DDTek.XmlConverter; namespace ConverterOne { class Program { static void Main(string[] args) { Console.Out.WriteLine(args[0] + " --> " + args[1]); Converter toXML = new ConverterFactory().CreateConvertFromXml ("converter:EDI"); toXML.Convert(new UriSource(args[0]), new UriResult(args[1])); } } }This program can be invoked from a command line as follows:
Creating XML Schemas from EDI
Following is a simple example that shows a C# program that generates XML Schema for EDIFACT version D07A; the name of the message being converted (in this case, ORDERS), is taken from the command line, which might look like this:
In this example, the XML Schema is written to the console.
using System; using DDTek.XmlConverter; namespace Example { public class CreateEdifactSchema { static void Main(String[] args) { String uri = "EDI:dialect=EDIFACT:version=D07A:long=yes:message=" + args[0]; try { ConverterFactory factory = new ConverterFactory(); SchemaGenerator schema = factory.CreateSchemaGenerator(uri); Result twr = new TextWriterResult(Console.Out); schema.GetSchema(twr); } catch (ConverterException ce) { Console.WriteLine(ce.Message); } } } }The previous example specified the dialect, version and message directly in the EDI: URI, using the dialect=, version= and message= properties:
For some file types, like EDI, you can instead supply a sample, or instance, document from which the XML Converters engine can read this information. When you use an EDI instance document, the schema generator generates an XML Schema for the dialect/version/message in that instance document.
In the following example, the name of the EDI instance document (data.edi) is taken from the command line, which might look like this:
In this example, the XML Schema is again written to the console:
using System; using DDTek.XmlConverter; namespace Example { public class CreateAnySchema { static void Main(String[] args) { String uri = "EDI:long=yes"; try { ConverterFactory factory = new ConverterFactory(); SchemaGenerator schema = factory.CreateSchemaGenerator(uri); Source us = new UriSource(args[0]); Result twr = new TextWriterResult(Console.Out); schema.GetSchema(us, twr); } catch (ConverterException ce) { Console.WriteLine(ce.Message); } } } }