using System.Globalization;
using NHapi.Model.V23.Message;
using System.Diagnostics;
namespace NHapiCreateMessageSimpleExample
static void Main(string[] args)
LogToDebugConsole("Creating ADT A01 message...");
var adtMessage = AdtMessageFactory.CreateMessage("A01");
var pipeParser = new PipeParser();
var xmlParser = new DefaultXMLParser();
LogToDebugConsole("Message was constructed successfully..." + "\n");
WriteMessageFile(pipeParser, adtMessage, "C:\\HL7TestOutputs", "testPipeDelimitedOutputFile.txt");
WriteMessageFile(xmlParser, adtMessage, "C:\\HL7TestOutputs", "testXmlOutputFile.xml");
LogToDebugConsole(string.Format("Error occured while creating HL7 message {0}", e.Message));
private static void WriteMessageFile(ParserBase parser, IMessage hl7Message, string outputDirectory, string outputFileName)
if (!Directory.Exists(outputDirectory))
Directory.CreateDirectory(outputDirectory);
var fileName = Path.Combine(outputDirectory, outputFileName);
LogToDebugConsole("Writing data to file...");
if (File.Exists(fileName))
File.WriteAllText(fileName, parser.Encode(hl7Message));
LogToDebugConsole(string.Format("Wrote data to file {0} successfully...", fileName));
private static void LogToDebugConsole(string informationToLog)
Debug.WriteLine(informationToLog);
namespace NHapiCreateMessageSimpleExample
public class AdtMessageFactory
public static IMessage CreateMessage(string messageType)
if (messageType.Equals("A01"))
return new OurAdtA01MessageBuilder().Build();
throw new ArgumentException(string.Format("{0} is not supported yet. Extend this if you need to", messageType));
namespace NHapiCreateMessageSimpleExample
internal class OurAdtA01MessageBuilder
private ADT_A01 _adtMessage;
var currentDateTimeString = GetCurrentTimeStamp();
_adtMessage = new ADT_A01();
CreateMshSegment(currentDateTimeString);
CreateEvnSegment(currentDateTimeString);
private void CreateMshSegment(string currentDateTimeString)
var mshSegment = _adtMessage.MSH;
mshSegment.FieldSeparator.Value = "|";
mshSegment.EncodingCharacters.Value = "^~\\&";
mshSegment.SendingApplication.NamespaceID.Value = "Our System";
mshSegment.SendingFacility.NamespaceID.Value = "Our Facility";
mshSegment.ReceivingApplication.NamespaceID.Value = "Their Remote System";
mshSegment.ReceivingFacility.NamespaceID.Value = "Their Remote Facility";
mshSegment.DateTimeOfMessage.TimeOfAnEvent.Value = currentDateTimeString;
mshSegment.MessageControlID.Value = GetSequenceNumber();
mshSegment.MessageType.MessageType.Value = "ADT";
mshSegment.MessageType.TriggerEvent.Value = "A01";
mshSegment.VersionID.Value = "2.3";
mshSegment.ProcessingID.ProcessingID.Value = "P";
private void CreateEvnSegment(string currentDateTimeString)
var evn = _adtMessage.EVN;
evn.EventTypeCode.Value = "A01";
evn.RecordedDateTime.TimeOfAnEvent.Value = currentDateTimeString;
private void CreatePidSegment()
var pid = _adtMessage.PID;
var patientName = pid.GetPatientName(0);
patientName.FamilyName.Value = "Mouse";
patientName.GivenName.Value = "Mickey";
pid.SetIDPatientID.Value = "378785433211";
var patientAddress = pid.GetPatientAddress(0);
patientAddress.StreetAddress.Value = "123 Main Street";
patientAddress.City.Value = "Lake Buena Vista";
patientAddress.StateOrProvince.Value = "FL";
patientAddress.Country.Value = "USA";
private void CreatePv1Segment()
var pv1 = _adtMessage.PV1;
pv1.PatientClass.Value = "O";
var assignedPatientLocation = pv1.AssignedPatientLocation;
assignedPatientLocation.Facility.NamespaceID.Value = "Some Treatment Facility";
assignedPatientLocation.PointOfCare.Value = "Some Point of Care";
pv1.AdmissionType.Value = "ALERT";
var referringDoctor = pv1.GetReferringDoctor(0);
referringDoctor.IDNumber.Value = "99999999";
referringDoctor.FamilyName.Value = "Smith";
referringDoctor.GivenName.Value = "Jack";
referringDoctor.IdentifierTypeCode.Value = "456789";
pv1.AdmitDateTime.TimeOfAnEvent.Value = GetCurrentTimeStamp();
private static string GetCurrentTimeStamp()
return DateTime.Now.ToString("yyyyMMddHHmmss",CultureInfo.InvariantCulture);
private static string GetSequenceNumber()
const string facilityNumberPrefix = "1234";
return facilityNumberPrefix + GetCurrentTimeStamp();