Table of Contents

Class Parser

Namespace
EDIParser
Assembly
EDIParser.dll

Provides the base implementation for parsing, navigating, modifying, and rebuilding EDI messages.

public class Parser : Component, IComponent, IDisposable
Inheritance
Parser
Implements
Derived
Inherited Members

Remarks

The parser converts an EDI message into a hierarchical object model consisting of segments, fields, components, repetitions, and subcomponents.

Derived parser classes configure the delimiters and parser type required for a particular EDI standard, such as X12, HL7, or EDIFACT.

Collection positions and segment indexes are one-based to preserve the behavior of the original VB implementation.

Constructors

Parser()

Initializes a new instance of the Parser class.

public Parser()

Properties

ComponentSeparator

Gets or sets the delimiter used to separate components within a field.

public string ComponentSeparator { get; set; }

Property Value

string

ConserveMemory

Gets or sets a value indicating whether parsed segments are removed from the parser after the ParsedSegment event is raised.

public bool ConserveMemory { get; set; }

Property Value

bool

true to clear accumulated segments after each event; otherwise, false.

Remarks

This option supports stream-oriented processing where the caller handles each segment through ParsedSegment and does not need the complete message retained in memory.

Exceptions

ApplicationException

Thrown when the property is set to true and no ParsedSegment event handler is registered.

FieldSeparator

Gets or sets the delimiter used to separate fields within a segment.

public string FieldSeparator { get; set; }

Property Value

string

IgnoreMissingItem

Gets or sets a value indicating whether missing collection items return placeholder objects instead of throwing exceptions.

public bool IgnoreMissingItem { get; set; }

Property Value

bool

true to return placeholder items for missing collection entries; otherwise, false.

Remarks

Changing this property also updates the behavior of the parser's segment collection. Newly parsed child collections inherit the setting when they are created.

RepetitionSeparator

Gets or sets the delimiter used to separate repeated values.

public string RepetitionSeparator { get; set; }

Property Value

string

Remarks

HL7 uses this delimiter for repeated fields. X12 uses it for repeated component values.

SegmentParsingOption

Gets or sets the strategy used to divide a message into segments.

public Parser.SegmentParsingOptions SegmentParsingOption { get; set; }

Property Value

Parser.SegmentParsingOptions

One of the Parser.SegmentParsingOptions values.

Exceptions

ApplicationException

Thrown when the property is set to Buffered and no ParsedSegment event handler is registered.

SegmentSeparator

Gets or sets the delimiter used to separate segments in a message.

public virtual string SegmentSeparator { get; set; }

Property Value

string

Remarks

Derived parser classes normally configure this property for their corresponding EDI standard.

Segments

Gets the segments parsed from the current message.

public Segments Segments { get; }

Property Value

Segments

Remarks

Segment positions are one-based. String keys currently contain the one-based numeric position assigned during parsing.

SubComponentSeparator

Gets or sets the delimiter used to separate subcomponents.

public string SubComponentSeparator { get; set; }

Property Value

string

Remarks

Setting this property records that subcomponent parsing was explicitly configured. An empty separator disables subcomponent parsing.

ValueIndexer

Gets the indexer used to retrieve or update message values using EDI element paths.

public ValueIndexer ValueIndexer { get; }

Property Value

ValueIndexer

Methods

GetValue(string)

Gets a value from the first matching segment using an EDI element path.

public string GetValue(string element)

Parameters

element string

The EDI element path identifying the requested value.

Returns

string

The value identified by the element path.

GetValue(string, int)

Gets a value from a specified segment using an EDI element path.

public string GetValue(string element, int segmentIndex)

Parameters

element string

The EDI element path identifying the requested value.

segmentIndex int

The one-based position of the segment.

Returns

string

The value identified by the path and segment index.

GetValue(string, int, int)

Gets a value from a specified segment and field repetition using an EDI element path.

public string GetValue(string element, int segmentIndex, int fieldRepeatIndex)

Parameters

element string

The EDI element path identifying the requested value.

segmentIndex int

The one-based position of the segment.

fieldRepeatIndex int

The one-based field-repetition position.

Returns

string

The value identified by the path, segment index, and repetition index.

Message()

Rebuilds the current EDI message from the parsed object model.

public virtual string Message()

Returns

string

The reconstructed EDI message, including a trailing segment separator when at least one segment is present.

Remarks

The message is generated from the current segment, field, component, repetition, and subcomponent values. Changes made through SetValue(string, string) or the object model are reflected in the returned message.

ParseMsg(string)

Parses an EDI message and populates the segment object model.

public virtual void ParseMsg(string msg)

Parameters

msg string

The EDI message to parse.

Remarks

Null characters are removed before parsing. Existing parsed segments are cleared before the new message is processed.

Segment, field, component, repetition, and subcomponent behavior depends on the delimiters and parser type configured by the derived parser.

When SegmentParsingOption is Buffered, segments are enumerated lazily and the ParsedSegment event is raised as each segment is completed.

Exceptions

ArgumentNullException

Thrown when msg is null.

InvalidOperationException

Thrown when the segment, field, or component separator has not been configured.

SetValue(string, string)

Sets a value using an EDI element path.

public void SetValue(string element, string value)

Parameters

element string

The EDI element path identifying the value to update.

value string

The value to assign.

SetValue(string, string, int)

Sets a value in a specified segment using an EDI element path.

public void SetValue(string element, string value, int segmentIndex)

Parameters

element string

The EDI element path identifying the value to update.

value string

The value to assign.

segmentIndex int

The one-based position of the segment.

SetValue(string, string, int, int)

Sets a value in a specified segment and field repetition using an EDI element path.

public void SetValue(string element, string value, int segmentIndex, int fieldRepeatIndex)

Parameters

element string

The EDI element path identifying the value to update.

value string

The value to assign.

segmentIndex int

The one-based position of the segment.

fieldRepeatIndex int

The one-based field-repetition position.

Events

ParsedSegment

Occurs after a segment has been parsed and added to the segment collection.

public event Parser.ParsedSegmentEventHandler? ParsedSegment

Event Type

Parser.ParsedSegmentEventHandler

Remarks

The event handler may set the cancellation argument to true to stop parsing the remaining message.