How to parse a HL7 message string properly?

6.3k views Asked by At

I'm using the NHAPI package to parse incoming message strings to HL7 IMessage interfaces using the PipeParser.

Based on the HL7 Minimum Layer Protocol (MLP) definition the HL7 message is always wrapped by some characters, 0x0b at start and 0x1c, 0x0d at the end. For testing purposes I'm sending this message to my application using the tool 7Edit.

MSH|^~\&|KISsystem|ZTM|NIDAklinikserver|HL7Proxy|201902271130||ADT^A01|68371142|P|2.3
EVN|A01|201902271130|201902271130
PID|1||677789||Aubertinó^Letizia||19740731|F||
PV1|1|O|||||||||||||||||456456456
IN1|1||999567890|gematik Musterkasse1GKV||||||||||||Prof. Dr.Aubertinó^Letizia||19740731|||||||||||201902271101|||||||X110173919

The debugged message string is

MSH|^~\&|KISsystem|ZTM|NIDAklinikserver|HL7Proxy|201902271130||ADT^A01|68371142|P|2.3
EVN|A01|201902271130|201902271130
PID|1||677789||Aubertin�^Letizia||19740731|F||
PV1|1|O|||||||||||||||||456456456
IN1|1||999567890|gematik Musterkasse1GKV||||||||||||Prof. Dr.Aubertin�^Letizia||19740731|||||||||||201902271101|||||||X110173919

Here you can see the characters wrapping the message. If you pass this message string to the PipeParser.Parse() method it will throw an exception with the message

Can't parse message beginning MSH|^~&|KISsystem|ZTM|NIDAklinikserver|HL7Proxy|

I think I will have to remove all those characters first. Is there something ready to use or do I have to store all those delimiters to a byte array, convert this byte array to a string and remove those delimiter strings from the message string?

1

There are 1 answers

2
Mikael On BEST ANSWER

never used NHAPI before now but I deal with HL7 messages and interfaces regularly.

You have to sanitize the data you're getting, so your assumption to remove the characters is correct. The characters count as whitespace so a simple Trim() works. We have clients that sometimes precede messages with special characters and double quotes randomly so that's always fun. But in this case the fix is pretty simple.

The below code parses the message on my end.

string message2 = @"MSH|^~\&|KISsystem|ZTM|NIDAklinikserver|HL7Proxy|201902271130||ADT^A01|68371142|P|2.3
                            EVN|A01|201902271130|201902271130
                            PID|1||677789||Aubertin�^Letizia||19740731|F||
                            PV1|1|O|||||||||||||||||456456456
                            IN1|1||999567890|gematik Musterkasse1GKV||||||||||||Prof. Dr.Aubertin�^Letizia||19740731|||||||||||201902271101|||||||X11017391";

var ourPP = new PipeParser();

try
{
    //exception is thrown without the Trim(); No exception with the Trim()
    var hl7Message = ourPP.Parse(message2.Trim());
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
Console.ReadKey();