Parsing EDI file with a single ISA envelope or with multiple ISA envelopes is exactly the same, you don't have to do anything specific.
The reader reads one item at a time and as long as you check when the next envelope begins, you can process all the following segments as part of that envelope:
using (var ediReader = new X12Reader(ediStream, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { var isa = ediReader.Item as ISA; if(isa != null) { // a new envelope begins }
Also, the ediReader maintains the current GS and ISA, hence for every message, regardless in which envelope, you can refer to its headers:
using (var ediReader = new X12Reader(ediStream, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { var isa = ediReader.CurrentInterchangeHeader; // the current ISA if X12 var gs = ediReader.CurrentGroupHeader // the current GS if X12
0
Patrick Gonzalez
I want to split a single EDI file with multiple ISA envelopes into multiple EDI files, each with a single ISA envelope. Is there an easy way to do this? Is there a property of ediReader that provides the entire segment text, so I can write the segment to a variable or text file?
0
Admin
Edited
You can split a multi-envelope file to single envelope files like this:
Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + @"my file with multiple envelopes"); using (var ediReader = new X12Reader(ediStream, TemplateFactory)) { int i = 0; var filePath = $"my_separate_envelope_file_{i++}.txt";
while (ediReader.Read()) { if (ediReader.Item is ISA isa) filePath = $"my_isa_{isa.InterchangeControlNumber_13}_{i++}.txt";
if (ediReader.Item is EdiMessage msg) File.AppendAllText(filePath, msg.ToEdi(new X12WriterSettings(ediReader.Separators)));
if (ediReader.Item is ControlSegment controlSegment) File.AppendAllText(filePath, controlSegment.ToEdi(ediReader.Separators)); } }
0
Patrick Gonzalez
This'll work for me. Thank you!
0
Admin
No problem, just updated the snippet to account for the current separators as well, as some may be different than the default.
0
Patrick Gonzalez
Thanks. By the way, the original syntax of your snippet didn't compile for me, so I changed it based on other code examples I've seen. I changed it to something more like this:
Comments
6 comments
Hello,
Parsing EDI file with a single ISA envelope or with multiple ISA envelopes is exactly the same, you don't have to do anything specific.
The reader reads one item at a time and as long as you check when the next envelope begins, you can process all the following segments as part of that envelope:
Also, the ediReader maintains the current GS and ISA, hence for every message, regardless in which envelope, you can refer to its headers:
I want to split a single EDI file with multiple ISA envelopes into multiple EDI files, each with a single ISA envelope. Is there an easy way to do this? Is there a property of ediReader that provides the entire segment text, so I can write the segment to a variable or text file?
You can split a multi-envelope file to single envelope files like this:
This'll work for me. Thank you!
No problem, just updated the snippet to account for the current separators as well, as some may be different than the default.
Thanks. By the way, the original syntax of your snippet didn't compile for me, so I changed it based on other code examples I've seen. I changed it to something more like this:
ISA isa = ediReader.Item as ISA;
if (isa != null)
{
//Do Stuff
}
EdiMessage msg = ediReader.Item as EdiMessage;
if (msg != null)
{
File.AppendAllText(fileName, msg.ToEdi(new X12WriterSettings(ediReader.Separators)));
}
ControlSegment ctlSeg = ediReader.Item as ControlSegment;
if (ctlSeg != null)
{
File.AppendAllText(fileName, ctlSeg.ToEdi(ediReader.Separators));
}
Please sign in to leave a comment.