-
Notifications
You must be signed in to change notification settings - Fork 5
Parsing an existing HL7 message
Message message = new Message(strMsg);
// Parse this message
bool isParsed = false;
try
{
isParsed = message.ParseMessage();
}
catch(Exception ex)
{
// Handle the exception
}
If the HL7 message is coming from a MLLP connection (see the official documentation), the message needs to be cleared from the MLLP prefixes and suffixes. Also, consider there can be more than one message in a single MLLP frame.
For this purpose, there is an ExtractMessages()
method, to be used as follows:
// extract the messages from a buffer containing a MLLP frame
var messages = MessageHelper.ExtractMessages(buffer);
// construct and process each message
foreach (var strMsg in messages)
{
Message message = new Message(strMsg);
message.ParseMessage(); // Required by most operations
// do something with the message object
}
Under certain conditions, it may be convenient to bypass the validation of the HL7 message by setting a flag in the ParseMessage()
method, as shown:
Message message = new Message(strMsg)
message.ParseMessage(true);
A message can be serialized as a string by using the Message.SerializeMessage()
method. The method accepts an optional boolean argument for enabling validation before serializing.
This flag is useful for some scenarios where it is needed to validate a message against its original value.
List<Segment> segList = message.Segments();
For example if there are multiple IN1 segments
List<Segment> IN1List = message.Segments("IN1");
Note index 1 will return the 2nd element from list
Segment IN1_2 = message.Segments("IN1")[1];
int countIN1 = message.Segments("IN1").Count;
Segment IN1 = message.DefaultSegment("IN1");
// OR
Segment IN1 = message.Segments("IN1")[0];
string SendingFacility = message.GetValue("MSH.4");
// OR
string SendingFacility = message.DefaultSegment("MSH").Fields(4).Value;
// OR
string SendingFacility = message.Segments("MSH")[0].Fields(4).Value;
string ContactPhone = message.GetValue("NK1(2).5"); // Second occurrence of NK1
bool isComponentized = message.Segments("PID")[0].Fields(5).IsComponentized;
// OR
bool isComponentized = message.IsComponentized("PID.5");
bool isRepeated = message.Segments("PID")[0].Fields(3).HasRepetitions;
// OR
bool isRepeated = message.HasRepetitions("PID.3");
List<Field> repList = message.Segments("PID")[0].Fields(3).Repetitions();
Field PID3_R2 = message.GetValue("PID.3[2]");
// OR
Field PID3_R2 = message.GetValue("PID.3(2)");
string version = message.Version;
string msgControlID = message.MessageControlID;
string messageStructure = message.MessageStructure;
string PatName1 = message.GetValue("PID.5.1");
// OR
string PatName1 = message.Segments("PID")[0].Fields(5).Components(1).Value;
bool isSubComponentized = message.Segments("PV1")[0].Fields(7).Components(1).IsSubComponentized;
// OR
bool isSubComponentized = message.IsSubComponentized("PV1.7.1");
Null elements (fields, components or subcomponents), also referred to as Present But Null, are expressed in HL7 messages as double quotes, like (see last field):
EVN|A04|20110613083617||""
Whenever requested individually, those elements are returned as null
, rather than double quotes:
var expectEmpty = evn.Fields(3).Value; // Will return an empty string
var expectNull = evn.Fields(4).Value; // Will return null
If this behavior is not desirable, it can be disabled by setting Encoding.PresentButNull
to null
before parsing:
var message = new Message(msg);
message.Encoding.PresentButNull = null;
message.ParseMessage();
A couple of date handling methods have been added, for parsing elements containing valid date/times, including time zones, as described in the HL7 standard. Examples:
// With time zone
string value1 = "20151231234500.1234+2358";
TimeSpan offset;
DateTime? dt1 = MessageHelper.ParseDateTime(value1, out offset);
// Date/time only
string value2 = "20151231234500";
DateTime? dt2 = MessageHelper.ParseDateTime(value2);
ParseDateTime
will catch exceptions by default and return null
in case of invalid dates. For preventing this mechanism, add an extra argument as true
, like:
try
{
var dt1 = MessageHelper.ParseDateTime(value1, out TimeSpan offse, true);
var dt2 = MessageHelper.ParseDateTime(value2, true);
}
catch
{
// do something here
}