private static void TraversAllSegments(Interchange interchange, IReadOnlyList<Segment> segments, Errors errors) { if (segments == null || segments.Count == 0) { errors.NewError("No segments provided for parsing."); return; } Group currentGroup = null; Document currentDocument = null; if ( !errors.Assert(() => segments[0].Name == "ISA", "FATAL Error: The first segment ('" + segments[0].Name + "') is not ISA segment.")) return; // FATAL error interchange.ISASegment = new ISASegment(segments[0].Elements); for (var i = 1; i < segments.Count; i++) { switch (segments[i].Name) { case "ST": currentDocument = new Document(); currentDocument.STSegment = new STSegment(segments[i].Elements); break; case "SE": if (currentDocument != null) { currentDocument.SESegment = new SESegment(segments[i].Elements); if (currentGroup != null) currentGroup.Documents.Add(currentDocument); else errors.NewError(string.Format("Segment:'{0}' placed before GS segment", segments[i].AsString)); currentDocument = null; } else errors.NewError(string.Format("Segment:'{0}' placed before ST segment", segments[i].AsString)); break; case "GS": currentGroup = new Group(); currentGroup.GSSegment = new GSSegment(segments[i].Elements); break; case "GE": if (currentGroup != null) { currentGroup.GESegment = new GESegment(segments[i].Elements); interchange.Groups.Add(currentGroup); currentGroup = null; } else errors.NewError(string.Format("Segment:'{0}' placed before GS segment", segments[i].AsString)); break; case "IEA": interchange.IEASegment = new IEASegment(segments[i].Elements); break; default: if (currentDocument != null) currentDocument.Segments.Add(segments[i]); else errors.NewError(string.Format("Segment:'{0}' placed before ST segment", segments[i].AsString)); break; } } // Validate a new interchange: interchange.Validate(errors); }