Documentation

C# .NET examples

Article author
Admin
  • Updated

EdiFabric 11.0.0 is a .NET SDK that parses, generates, validates, acknowledges, and splits EDI files. The C# examples are Visual Studio solutions, one repository per EDI standard, that show those features with sample files you can run.

EdiFabric does not include communication components (AS2 or SFTP), a dashboard, or a UI. It is a library you call from your own application.

Sign up free for Community   Browse the examples on GitHub

Start here

  1. Clone the repository for your standard. Each repo has a NET 6 solution and a NET Framework 4.8 solution that compile the same sources.
  2. Sign up free for Community and copy your serial key from Your Account. Community never expires, needs no credit card, and is limited to 250 operations per day for non-production use.
  3. Paste that serial into TrialSerialKey in Config.cs. The file lives under NET Framework 4.8/…Common/Config.cs. The .NET 6 projects link this file, so one edit covers both solutions.
  4. Open the solution, set the startup project from the table below, and run it. Or from the repo root:

    cd "NET 6/EdiFabric.Examples.X12.Demo"
    dotnet run

NuGet restore pulls EdiFabric 11.0.0 and the template packages the solution needs (version 3.0.0). Rebuild with package restore enabled. If a package is missing, install it from NuGet.

Pick a repository

Open the README in the repository you cloned. It lists every sample project, the file the demo reads, and the same licensing steps as this article.

Standard Repository Startup project
X12 4010 and HIPAA 5010 X12.NET EdiFabric.Examples.X12.Demo
EDIFACT, EANCOM, IATA PADIS, EDIGAS EDIFACT.NET EdiFabric.Examples.EDIFACT.Demo
HL7 2.6 HL7.NET EdiFabric.Examples.HL7.Demo
NCPDP Telecommunication D.0 NCPDP.NET EdiFabric.Examples.NCPDP.Telco.Demo
NCPDP SCRIPT 10.6 SCRIPT.NET EdiFabric.Examples.NCPDP.Script.Demo
VDA VDA.NET EdiFabric.Examples.VDA.Read
Flat files and CSV FlatFile.NET EdiFabric.Examples.FlatFile.Read

Requirements

  • Visual Studio 2022, or the .NET SDK. Download Visual Studio.
  • .NET 6 for the NET 6 solution. The projects set net6.0 so they stay compatible with existing .NET 6 apps. EdiFabric 11.0.0 also provides net8.0, net9.0, and net10.0. To evaluate a later version, change TargetFramework in the project file and rebuild.
  • .NET Framework 4.8 for the NET Framework 4.8 solution.
  • See also the supported .NET versions.

Serial key

Community is the evaluation license. Sign up at edifabric.com/pricing, then copy the serial from Your Account into Config.TrialSerialKey.

Community never expires and requires no credit card. It is for non-production evaluation, learning, and prototyping, at 250 operations per day. One operation is one parse, generate, validate, or acknowledge call. The quota is shared across ediFabric .NET, Native, and Cloud. If you hit it, calls throw LicenseException with error 639.

Use of the product is subject to the EULA. Moving to a paid plan is a serial-key swap in the same Config.cs field. Community keeps calling License.SetSerial. On Developer or Enterprise, switch to the call in the next section.

Example for X12. Other repositories use the same property, in EdiFabric.Examples.{Standard}.Common/Config.cs (NCPDP uses EdiFabric.Examples.NCPDP.Telco.Common and EdiFabric.Examples.NCPDP.Script.Common).

public static string TrialSerialKey = "your-community-serial-key";

Licensing

Every example calls License.SetSerial before it reads or writes. On Community that is the call to use.

Plan What works Call this
Community License.SetSerial only. Online check. 250 operations per day. Non-production. License.SetSerial
Developer License.SetSerial and License.EnsureToken. EnsureToken caches the result for 1 day. License.EnsureToken
Enterprise License.SetSerial, License.GetToken, and License.SetToken. License.SetToken (offline tokens)
// Community: authorize against the license server
License.SetSerial(serial);

// Developer (recommended): 1-day built-in cache; refreshes if the token expires within N seconds
License.EnsureToken(serial, seconds: 3600);

// Enterprise: set an offline token
License.SetToken(token);

The examples call License.SetSerial(Config.TrialSerialKey). On Developer, call License.EnsureToken instead. TokenFileCache.Set() in the Common project is the manual GetToken / SetToken cache, for when you want to store the token yourself.

Enterprise offline tokens are issued after purchase. During an Enterprise trial, use the cached path (License.EnsureToken). Compare plans.

Read a file

The X12 demo reads Files/X12/PurchaseOrders.txt with EdiFabric.Templates.X12 and Files/HIPAA/ClaimPayment.txt with EdiFabric.Templates.Hipaa. It parses every transaction with X12Reader and validates each one with IsValid. Set a breakpoint at the end of Translate_X12_4010 or Translate_HIPAA_5010 and inspect ediItems. To translate your own file, change the path in EdiFabric.Examples.X12.Demo/Program.cs.

using EdiFabric.Core.Model.Edi;
using EdiFabric.Framework.Readers;
using EdiFabric.Templates.X12004010;

License.SetSerial(serial);   // from your Community or paid plan

var ediStream = File.OpenRead(@"Files\X12\PurchaseOrders.txt");

List<IEdiItem> ediItems;
using (var ediReader = new X12Reader(ediStream, "EdiFabric.Templates.X12"))
    ediItems = ediReader.ReadToEnd().ToList();

var purchaseOrders = ediItems.OfType<TS850>();

X12Reader takes the stream and the template assembly name (EdiFabric.Templates.X12 or EdiFabric.Templates.Hipaa). ReadToEnd loads the interchange into memory. For large files, use the streaming samples in ReadEDI.

The other standards follow the same shape. Swap the reader, the assembly name, and the message type:

Standard Reader Assembly name Example type
X12 X12Reader EdiFabric.Templates.X12 TS850
HIPAA X12Reader EdiFabric.Templates.Hipaa HIPAA transaction class
EDIFACT EdifactReader EdiFabric.Templates.Edifact TSORDERS
HL7 Hl7Reader EdiFabric.Templates.Hl7 TSRDSO13
NCPDP Telecommunication NcpdpTelcoReader EdiFabric.Templates.Ncpdp TSB1
NCPDP SCRIPT NcpdpScriptReader EdiFabric.Templates.Ncpdp TSNEWRX
VDA VdaReader EdiFabric.Templates.Vda TS4905

VDA and flat files have no envelope that names the message. Those readers take a factory that maps the start of a record to a .NET type. The VDA and flat-file READMEs show the factory.

Validate and acknowledge

After a transaction parses, IsValid checks it against the template. ValidateEDI shows custom codes, data types, and the envelope (ISA and GS for X12, UNB and UNG for EDIFACT). AcknowledgeEDI builds a 997 for X12, or a CONTRL for EDIFACT, for a valid group, an invalid group, and duplicates.

foreach (var message in ediItems.OfType<EdiMessage>())
{
    if (message.HasErrors)
        continue;

    MessageErrorContext mec;
    if (!message.IsValid(out mec))
    {
        var validationIssues = mec.Flatten();
    }
}

Write EDI

WriteEDI builds an interchange with X12Writer: ISA, then GS, then the transaction. The same project covers custom delimiters, a postfix after each segment, batches, and empty data elements. EDIFACT uses EdifactWriter (UNB, then the message). HL7, NCPDP, VDA, and flat files have their own writers in those repositories.

using (var stream = new MemoryStream())
{
    using (var writer = new X12Writer(stream))
    {
        writer.Write(SegmentBuilders.BuildIsa("1"));
        writer.Write(SegmentBuilders.BuildGs("1"));
        writer.Write(invoice);
    }
}

Examples by feature

X12 project names are below. EDIFACT uses the same suffixes under EdiFabric.Examples.EDIFACT.*. HL7 names the projects ReadHL7, WriteHL7, and ValidateHL7. NCPDP uses ReadNCPDP, WriteNCPDP, and ValidateNCPDP. VDA and flat files ship read and write projects. The README in each repository is the full list.

Project What it shows
EdiFabric.Examples.X12.Demo Read an X12 4010 file and a HIPAA 5010 file, then validate each transaction
EdiFabric.Examples.X12.ReadEDI Read to end, stream, batch, split on a repeating loop, corrupt files, partner templates, custom ISA/GS
EdiFabric.Examples.X12.WriteEDI Write to a stream or file, delimiters, new lines, batches, empty elements, no auto trailers
EdiFabric.Examples.X12.ValidateEDI Validate after read and before write, custom codes, data types, ISA and GS, 810, 850, and 837P
EdiFabric.Examples.X12.AcknowledgeEDI Generate and read 997
EdiFabric.Examples.X12.JSON Serialize and deserialize JSON
EdiFabric.Examples.X12.XML XmlSerializer and DataContractSerializer
EdiFabric.Examples.X12.CSV Import and export CSV
EdiFabric.Examples.X12.DB Save and reload an 850 with EF Core (create the database before you run it)
EdiFabric.Examples.X12.T837P.DB Save and reload an 837P with EF Core (create the database before you run it)
EdiFabric.Examples.X12.MapEDI Map with AutoMapper and XSLT
EdiFabric.Examples.X12.ModifyTemplates Parse a partner-specific 850 template
EdiFabric.Examples.X12.Templates Example EDI templates

Examples by message type

Each message project reads a sample file and writes the same transaction back out. X12.NET includes 210, 214, 404, 810, 824, 832, 850, 855, 856, 857, 861, 945, and the HIPAA 270, 271, 276, 277, 278, 820, 834, 835, 837D, 837I, and 837P samples. EDIFACT.NET includes ORDERS, ORDRSP, INVOIC, DESADV, IFTMIN, IFTSTA, PRICAT, plus CUSCAR, PAXLST, BAPLIE, PNRGOV, EANCOM INVOIC, and EDIGAS NOMINT.

The full project tables are in the X12 README and the EDIFACT README.

EDI templates

The models published on NuGet, such as EdiFabric.Templates.X12, EdiFabric.Templates.Hipaa, EdiFabric.Templates.Edifact, EdiFabric.Templates.Hl7, EdiFabric.Templates.Ncpdp, EdiFabric.Templates.Padis, EdiFabric.Templates.Vda, and EdiFabric.Templates.Edigas, are for evaluation only. They are a Community plan limitation. The examples reference them so you can run the samples on Community.

Paid plans provide every template as plain C# files. Add them to the solution by following How to create EDI template projects. For evaluation and the Community plan, you can still download the templates in compiled form by following the same article. Flat-file models are already C# classes in EdiFabric.Examples.FlatFile.Common.

The same classes validate as well as parse. If a transaction is missing, ask for it. Browse versions without an account in the EdiNation spec library.

Class names follow TS plus the transaction id:

  • EdiFabric.Templates.X12004010.TS + the transaction id, such as 810 or 850.

    EdiFabric.Templates.X12004010.TS810
  • EdiFabric.Templates.EdifactD96A.TS + the message name, such as INVOIC or ORDERS.

    EdiFabric.Templates.EdifactD96A.TSINVOIC
  • EdiFabric.Templates.Hl726.TS + the message, such as ADTA01 or RDSO13.

    EdiFabric.Templates.Hl726.TSADTA01
  • EdiFabric.Templates.TelcoD0.TS + the transaction, such as B1 or B2.

    EdiFabric.Templates.TelcoD0.TSB1
  • EdiFabric.Templates.Script106.TS + the message, such as NEWRX or RXFILL.

    EdiFabric.Templates.Script106.TSNEWRX
  • EdiFabric.Templates.Vda.TS + the message id, such as 4905 or 4908.

    EdiFabric.Templates.Vda.TS4905

Versions included with the examples:

Custom samples in EDIFACT.NET also cover US Customs CUSCAR and PAXLST (D03B) and SMDG BAPLIE (D13B). For another version on a paid plan, add that model as C# files using the template-project article above.

Error codes

License failures throw LicenseException. ErrorCode is the number below, and Message is the text.

Error 639 means the Community daily quota was exceeded. Upgrade at edifabric.com/pricing if you want to continue. Error 635 means no serial or token was set. Paste your Community serial into TrialSerialKey and call License.SetSerial.

Code Message
620 The token is invalid. Contact support@edifabric.com for assistance
628 The serial number is missing or incorrect. GetToken doesn't work with developer license. Contact support@edifabric.com for assistance
629 License was not installed. Contact support@edifabric.com for assistance
630 No license to use this version. Contact support@edifabric.com for assistance
631 The token has expired. Get and set a new token to continue. Contact support@edifabric.com for assistance
632 The token is missing. Set token to continue. Contact support@edifabric.com for assistance
633 Reached the maximum number of licenses. Set token to continue. Contact support@edifabric.com for assistance
634 Environment not recognized for licensing or reached the maximum number of licenses. Contact support@edifabric.com for assistance
635 Serial or token not found. Either set token or serial to continue. Contact support@edifabric.com for assistance
636 The rate to get serials was exceeded for your license. Wait for 60 seconds and try again or upgrade your license. Contact support@edifabric.com for assistance
638 The operation is not supported by your license
639 Your license has reached its daily call limit. Upgrade your plan at edifabric.com to continue using the product.

Parser, validation, and buffer codes (1, 501, and 611 through 627, plus 637) are listed in the README of the repository you cloned, for example the X12 error codes.

Warranty

The source code in these example projects is strictly for demonstrational purposes and is provided "AS IS" without warranty of any kind, whether expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Share this:

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.