Search code examples
c#hl7nhapi

Parsing an HL7 without a priori messageType knowledge


In NHapi, how can we parse a message if we don't know what messageType (MSH#9) it is?

var parser = new NHapi.Base.Parser.PipeParser();

IMessage parsedMessage = parser.Parse(SampleMessage);

parsedMessage is a NHapi.Base.Model.GenericMessage.V25 at runtime and I can't seem to read in the MSH header to read the MessageType field and then re-parse(?) the message as that message type.

I'm frustrated by the lack of documentation and examples. Perhaps I'm very far off base. I am very new to HL7, but I thought I was doing well understanding the HL7 spec until I tried using NHapi.


Solution

  • parsedMessage.GetStructureName() will give you the message type and trigger event. parser.Encode(parsedMessage) will give you the message back in pipe-delimited format.

    The following code shows how to get the message type and also how to get the original message in pipe format.

    public static String ParseMessage(String message)
    {
        var parser = new NHapi.Base.Parser.PipeParser();
        var parsedMessage = parser.Parse(message);
    
        //Get the message type and trigger event
        var msgType = parsedMessage.GetStructureName();
    
        //Get the message in raw, pipe-delimited format
        var pipeDelimitedMessage = parser.Encode(parsedMessage);
    
        return pipeDelimitedMessage;
    }
    

    Some good starter code can be found at the hapi examples site.