Can you use C# to process EDI messages without any libraries? Yes, as EDI is pure text
Posted: (EET/GMT+2)
Electronic Data Interchange (EDI) is often seen as something that requires special tools and libraries to process. Many of these happen to be commercial. In reality, most EDI formats are just structured text. This means you can process many scenarios with plain C# and some careful string handling, especially if you control or know the structure of the messages.
Let's take a tiny, simplified EDIFACT order message. In many EDIFACT
implementations, the segment terminator is a single quote character ('):
UNH+1+ORDERS' BGM+220+12345' DTM+137:20230101:102' NAD+BY+123456789' LIN+1+PRODUCT1:9' QTY+21:5' UNZ+1+1'
For a simple internal integration, we might want to extract just a few fields,
like the document number, buyer and quantity. Because the message is text, we
can split it into segments (this is what EDI calls them) by the ' character and then handle each segment individually.
public class EdiOrder
{
public string? DocumentNumber { get; set; }
public string? BuyerCode { get; set; }
public string? ProductCode { get; set; }
public int? Quantity { get; set; }
}
public static EdiOrder ParseOrder(string ediMessage)
{
EdiOrder order = new EdiOrder();
// split by segment terminator (')
string[] segments = ediMessage
.Split('\'', StringSplitOptions.RemoveEmptyEntries);
foreach (string rawSegment in segments)
{
string segment = rawSegment.Trim();
if (segment.Length == 0) continue;
// split by +
string[] parts = segment.Split('+');
switch (parts[0])
{
case "BGM":
// BGM+220+12345
if (parts.Length > 2)
order.DocumentNumber = parts[2];
break;
case "NAD":
// NAD+BY+123456789
if (parts.Length > 2 && parts[1] == "BY")
order.BuyerCode = parts[2];
break;
case "LIN":
// LIN+1+PRODUCT1:9
if (parts.Length > 2)
{
var itemParts = parts[2].Split(':');
if (itemParts.Length > 0)
order.ProductCode = itemParts[0];
}
break;
case "QTY":
// QTY+21:5
if (parts.Length > 2)
{
var qtyParts = parts[2].Split(':');
if (qtyParts.Length > 1 &&
int.TryParse(qtyParts[1], out var qty))
{
order.Quantity = qty;
}
}
break;
}
}
return order;
}
This is intentionally simplified, but it shows the main idea: EDI is text, and if you know the delimiters and segment structure, you can parse it with normal string operations and some small helper methods.
For serious production use, you probably still want a proper EDI library, especially for handling all edge cases, acknowledgements and different message types. But if you have a controlled subset of EDI messages, C# alone is often enough for lightweight integrations.
It's a good reminder that many "enterprise" formats are still, at the end of the day, just text. Think about XML, JSON, CSV, EDI, and you get the drift.
In one of my projects, this approach now processes hundreds of EDI order messages per day. Originally, the system used an expensive commercial EDI component, but the custom C# implementation ended up being vastly faster, roughly 100 messages per second. Sometimes the most efficient solution is the simplest one.
Happy EDI processing!