Skip to content
Jake Aitchison edited this page Jan 20, 2023 · 5 revisions

Encoding a message

Below is very basic example of how to encode a HL7 message.

var parser = new NHapi.Base.Parser.PipeParser();
var qry = new NHapi.Model.V231.Message.QRY_R02();

qry.MSH.MessageType.MessageType.Value = "QRY";
qry.MSH.MessageType.TriggerEvent.Value = "R02";
qry.MSH.MessageType.MessageStructure.Value = "QRY_R02";
qry.MSH.FieldSeparator.Value = "|";
qry.MSH.SendingApplication.NamespaceID.Value = "CohieCentral";
qry.MSH.SendingFacility.NamespaceID.Value = "COHIE";
qry.MSH.ReceivingApplication.NamespaceID.Value = "Clinical Data Provider"; 
qry.MSH.ReceivingFacility.NamespaceID.Value = "facility";
qry.MSH.EncodingCharacters.Value = @"^~\&";
qry.MSH.VersionID.VersionID.Value = "2.3.1";
qry.MSH.DateTimeOfMessage.TimeOfAnEvent.SetLongDate(DateTime.Now);
qry.MSH.MessageControlID.Value = "messageControlId";
qry.MSH.ProcessingID.ProcessingID.Value = "P";

var st = qry.QRD.GetWhoSubjectFilter(0);
st.AssigningAuthority.UniversalID.Value = "facility";
st.IDNumber.Value = "mrn";
qry.QRD.QueryDateTime.TimeOfAnEvent.SetLongDate(DateTime.Now);
qry.QRD.QueryFormatCode.Value = "R";
qry.QRD.QueryPriority.Value = "I";

var what = qry.QRD.GetWhatSubjectFilter(0);
what.Identifier.Value = "RES";

return parser.Encode(qry);