/// <summary>Constructor for instantiation when EDI data string is included</summary> public Interchange(string edi) { FormattingOptions = InterchangeControlHeader.GetFormattingOptionsFromHeading(edi); if (edi.Length > 3 && edi.Substring(0, 3).Equals(InterchangeControlHeader.ElementName)) { Parse(edi, FormattingOptions); } else { throw new ArgumentException("EDI is not a valid interchange"); } }
/// <summary> /// Initialization method for when EDI data string is known /// and the segment seperator if the interchange is known /// </summary> private void Parse(string value, FormattingOptions options) { var segmentStringValueCollection = value.Split(options.SegmentTerminator.AsSplitDelimiter(), StringSplitOptions.None); var functionalGroupStringArray = new List <System.Text.StringBuilder>(); foreach (var segment in segmentStringValueCollection) { var segmentStringInstance = segment; segmentStringInstance = RemoveCharacterFromString(segmentStringInstance, "\r", options); segmentStringInstance = RemoveCharacterFromString(segmentStringInstance, "\n", options); if (segmentStringInstance.Length < 3) { continue; } switch (segmentStringInstance.Substring(0, 3)) { case InterchangeControlHeader.ElementName: Header = new InterchangeControlHeader(segmentStringInstance, options); break; case InterchangeControlTrailer.ElementName: Trailer = new InterchangeControlTrailer(segmentStringInstance, options); break; default: if (segmentStringInstance.Substring(0, 2).Equals(FunctionalGroupHeader.ElementName)) { functionalGroupStringArray.Add(new System.Text.StringBuilder()); } functionalGroupStringArray[functionalGroupStringArray.Count - 1].Append(segmentStringInstance); functionalGroupStringArray[functionalGroupStringArray.Count - 1].Append(options.SegmentTerminator); break; } } foreach (var functionalGroupString in functionalGroupStringArray) { var fg = new FunctionalGroup.FunctionalGroup(functionalGroupString.ToString(), FormattingOptions); FunctionalGroups.Add(fg); } FunctionalGroups.TrimExcess(); }