Search This Blog

2009-04-22

HL7 Parsing using .NET

Free HL7 Parser:
The HL7(Health Level 7) message is very familior to the people who are working on HealthCare Projects.It is like a plain txt file with some seperator and contain verious kind of message like patient details,Admit/Visit Notification,Pharmacy Details etc.But there can be multi level seperator.The message structure are very complex,So its not so easy to parse the message programatically.
HL7 message can be parsed easily from .NET by the help of nHAPI.This are free dll.

Download nHapi

How to use...
If you like to parse the HL7 files programatically,you first want to know the Message structure of that particular message type of HL7.HL7 message have different version (like 2.1,2.5,2.5.1,2.6 etc) and each version have different message type like(ADT_A01,ADT_A02,ADT_A03).ADT_A01 message describes Admit/Visit Notification,ADT_A01 describes Transfer a patient etc.To describes the details of each message type is out of the scope.
Now using nHAPI you can extract the fields of a HL7 message.
I am writing some code which will extract some fields like PID,FamilySurname,GivenName,MiddleInitial,DOB,Sex .I have taken some V 2.5 message for this example.

Message type:ADT_A01

Message:MSH^~\&7EDIT7EDIT.COM20090211120829ADT^A01P2.5EVNA01198808181123PIDPATID1234^5^M11JONES^WILLIAM^A^III19610615MC1200 N ELM STREET^^GREENSBORO^NC^27401-1020GL(919)379-1212(919)271-3434~(919)277-3114SPATID12345001^2^M101234567899-87654^NCNK11JONES^BARBARA^KWIFE20011105NK11JONES^MICHAEL^AFATHERPV11I2000^2012^01004777^LEBAUER^SIDNEY^J.SUR-7A0AL11^PENICILLINPRODUCES HIVES~RASHAL12^CAT DANDERDG1001I91550MAL NEO LIVER, PRIMARY19880501103005FPR12234M11111^CODE151COMMON PROCEDURES198809081123ROL45^CANAB^ROLE MASTER LISTADROLKATE^SMITH^ELLEN199505011201GT111221519BILL^GATES^AIN1001A3571234BCMD132987IN2ID1551001SSN12345678ROL45^CANNS^ROLE MASTER LISTADROLKATE^ELLEN199505011201

CODING:
Step 1.Take a reference of two following dlls from the .NET
NHapi.Model.V25,NHapi.Base
Step 2.From the cs page import the namespace

using NHapi.Base;
using NHapi.Base.Parser;
using NHapi.Base.Model;
using NHapi.Model.V25;
using NHapi.Model.V25.Message;
using NHapi.Model.V25.Segment;

Step 3.Create an object of PipeParser as the class level

PipeParser parser = new PipeParser();

Step 4.Write a method which will parse the HL7
public void getADT_A01()
{
IMessage m = parser.Parse(strMessage);//str message will contain your HL7 Message
ADT_A01 adtA01 = m as ADT_A01;

string strPID = adtA01.PID.GetPatientIdentifierList(0).IDNumber.ToString(); string strFamilySurName = adtA01.PID.GetPatientName(0).FamilyName.Surname.
ToString();
string strGivenName = adtA01.PID.GetPatientName(0).GivenName.ToString();
string strMiddleInitial = adtA01.PID.GetPatientName(0).SecondAndFurtherGivenNamesOrInitialsThereof.ToString();
string strDOB = adtA01.PID.DateTimeOfBirth.Time.ToString();
string strSex = adtA01.PID.AdministrativeSex.ToString();
}
Now strFamilySurName,strGivenName ,strMiddleInitial ,strDOB ,strSex contain the FamilySurName,GivenName ,MiddleInitial ,DOB ,Sex respectively

Now take another example of ADT_A02 message
Message version-V2.5
Message:
MSH^~\&7EDIT7EDIT.COM20090211151404ADT^A02P2.5ALNEEVNA02200007250712PID1191959Franz^Lotte^MarieSchulz19560129F171 ZOBERLEIN^^ISHPEMING^ ^49849(900)485-5344(900)485-5344371-66-9256New York^American20010131PV1130148^ADDISON^JAMES123434

Method:
public void getADT_A02()
{
IMessage m = parser.Parse(hl7BO.HL7Message);
ADT_A02 adtA02 = m as ADT_A02;
string strPID = adtA02.PID.GetPatientIdentifierList(0).IDNumber.ToString(); string strFamilySurName = adtA02.PID.GetPatientName(0).FamilyName.Surname.ToString(); string strGivenName= adtA02.PID.GetPatientName(0).GivenName.ToString(); string strMiddleInitial = adtA02.PID.GetPatientName(0).SecondAndFurtherGivenNamesOrInitialsThereof.ToString();
string strDOB = adtA02.PID.DateTimeOfBirth.Time.ToString();
string strSex= adtA02.PID.AdministrativeSex.ToString();
}

In this way You can parse each & every message of HL7

Now do the parsing of other type of HL7 message by yourself.All the best.
Please feel free if you have any problem.

15 comments:

Anonymous said...

hi Ranjan,

Nice post from you to use nHAPI,
but i have a small questions in your post.

you created: IMessage m = parser.Parse(strMessage)

can you explian the 'parse' object creation?

i am using V25 dll, it is having PipeParser and XMLParser, but i coundn't get Parse() with single argument (parameter).

Please help..

Regards,
Balakrishna.

Manab Ranjan Basu said...

Hi
Actually parse is not an object ,the object is parser

I am creating the parser object as
follows

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

Anonymous said...

HI,

First of all thank you very much Ranjan for your quick responce.

I followed what you explained but parser.Parse(strMessage) is expecting more(3) arguments but we are passing only one arugument (strmessage).

See the following compilation error:

No overload for method 'parse' takes '1' arguments.

Am I doing anything wrong? using v25.

Regards,
Bala.

Manab Ranjan Basu said...

Hi
I am sure you are doing wrong.
the parse method should be like Parse() not parse(),I mean P will be in caps.
:)
So just notice your Parse method.

Balakrishna Yeluvolu said...

Ranjan,

I found problem that is at my end, I created perser object in getADT_A01() instead at class level as you mentioned in the article. That problem was fixed thank you vary much for your help.
But i have still issues with it.

1. the sample message with v25 is giving error saying: Can't process message of version '27401-1020GL(919)379-1212(919)271-3434' - version not recognized

2. I used another test message that message validated successfully but at this staement
ADT_A01 adtA01 = m as ADT_A01;

'adtA01' becomes null.

My code as follows:

public void getADT_A01()
{
string strMessage = @"MSH^~\&7EDIT7EDIT.COM20090211120829ADT^A01P2.5EVNA01198808181123PIDPATID1234^5^M11JONES^WILLIAM^A^III19610615MC1200 N ELM STREET^^GREENSBORO^NC^27401-1020GL(919)379-1212(919)271-3434~(919)277-3114SPATID12345001^2^M101234567899-87654^NCNK11JONES^BARBARA^KWIFE20011105NK11JONES^MICHAEL^AFATHERPV11I2000^2012^01004777^LEBAUER^SIDNEY^J.SUR-7A0AL11^PENICILLINPRODUCES HIVES~RASHAL12^CAT DANDERDG1001I91550MAL NEO LIVER, PRIMARY19880501103005FPR12234M11111^CODE151COMMON PROCEDURES198809081123ROL45^CANAB^ROLE MASTER LISTADROLKATE^SMITH^ELLEN199505011201GT111221519BILL^GATES^AIN1001A3571234BCMD132987IN2ID1551001SSN12345678ROL45^CANNS^ROLE MASTER LISTADROLKATE^ELLEN199505011201";

IMessage m = parser.Parse(strMessage);//str message will contain your HL7 Message
ADT_A01 adtA01 = m as ADT_A01;

string strPID = adtA01.PID.GetPatientIdentifierList(0).IDNumber.ToString();
string strFamilySurName = adtA01.PID.GetPatientName(0).FamilyName.Surname.ToString();
string strGivenName = adtA01.PID.GetPatientName(0).GivenName.ToString();
string strMiddleInitial = adtA01.PID.GetPatientName(0).SecondAndFurtherGivenNamesOrInitialsThereof.ToString();
string strDOB = adtA01.PID.DateTimeOfBirth.Time.ToString();
string strSex = adtA01.PID.AdministrativeSex.ToString();
}

Please share your thoughts..

Josh said...

How do you extract fields from the Z segments using nHAPI? Is there a generic segment class?

Manab Ranjan Basu said...

Hi Josh
You can install 7Edit.By this,you can know the HL7 Structure and accordingly you can use the method of nHAPI,untill unless you know the structure of HL7 ,you can extract the feild using nHAPI.You can download it from the following link
http://www.7edit.com/home/index.php

Anonymous said...

Thank you for the sample code, this is really helpful. I'm just familiarizing myself with NHapi, and recently have run into a challenge while rendering values from OBX segments. Is there a way to render a collection of the OBX segments and iterate through the field values?

For example, I have a message type if ADT_A31 and it includes OBX segments within the HL7 file. I ‘m looking to identify all repeating segments equal to “OBX”, and render individual field values, respectively. I’m using NHapi.Model.V23.

Thank you in advance.

somu said...

Hi,
Does nHapi suppport HL7 v2.6.
If not which other parser in C# does support it.

Marc-Andre said...

how can i parse a hl7 message without any validation?

Zacherias Jose said...

Hi there,

I'm trying to parse an HL7 2.1 message using NNapi. But I'm getting an error while parsing "Can't process message of version '2.1' - version not recognized".

Please anyone help me to solve this issue.

Thanks in advance,

Justin

Anonymous said...

Does nHAPI allows us to generate HL7 messages? I know it supports to Parse the HL7 Message.

Jayant Jaiswal said...

Very very Helpful article
thanks very much

Pramod Gehlot said...

ADT_A01 adtA01 = m as ADT_A01;

'adtA01' becomes null.

Please help me
i am using 2.3 nersion
and
i can not parse message

Pramod Gehlot said...

ADT_A01 adtA01 = m as ADT_A01;

'adtA01' becomes null.


Please help

how to solve null problem