### HL7 V2 Message structure examples Source: https://github.com/angusmillar/peterpiper/wiki/0.-Introduction Examples illustrating the syntax of various HL7 V2 message components including messages, segments, elements, repeats, fields, components, subcomponents, and content. ```text MSH|^~\&|SUPER-LIS^2.16.840.1.113883.19.1^ISO|NE...etc ``` ```text ``` ```text |this is field 1 in Element 1~this is field 2 in Element 1| ``` ```text |Ths is a single Field|This is Field Repeat 1~This is Field Repeat 2| ``` ```text |Compoent1^Compoent2^Compoent3| ``` ```text |Compoent1^SubCompoent1&SubCompoent2&SubCompoent3^Compoent3| ``` ```text |This is content 0 not highlighted\H\ this is content 2 highlighted \N\ this is content 4 not highlighted| ``` -------------------------------- ### HL7 V2 message example Source: https://github.com/angusmillar/peterpiper/wiki/1.-Message A sample HL7 V2 message in pipe-delimited format showing MSH, PID, and PV1 segments. ```text MSH|^~\&|SUPER-LIS^2.16.840.1.113883.19.1^ISO|GOODPATH^4321^AUSNATA|Rhubarb-EMR^2.16.840.1.113883.19.4.2^ISO|GOODHOSP^2.16.840.1.113883.19.5^ISO|20150410112000+1000||ORU^R01^ORU_R01|P0000051504102331070|P|2.4|||AL|NE|AUS|8859/1 **PID**|1||2142363^^^GOODHOSP^MR~61405230941^^^AUSHIC^MC~WA123456B^^^AUSDVA^DVG~8003608833357361^^^AUSHIC^NI||PatientSurnameOne^FirstnameOne^MiddleNameOne^SufixOne^PrefixOne^^L~PatientSurnameTwo^FirstnameTwo^MiddleNameTwo^SufixTwo^PrefixTwo^^M||194506241031|M|||Unit 1^111 Nehta Street^Brisbane^^4000^AUS^H~Unit 2^222 NehtaTwo Street^Brisbane^^4000^AUS^B||^PRN^PH^^^^93235615|^WPN^CP^^^^0414778341 PV1|1|O|Ward1^RoomE8^Bed10^GOODHOSP&2.16.840.1.113883.19.5&ISO||||ABCB^DrASurname^DrAttending^^^Dr^^^SUPER-LIS~123456^DrASurname^DrAttending^^^Dr^^^GOODHOSP~2304227F^DrASurname^DrAttending^^^Dr^^^AUSHICPR|HIJK^DrASurname^DrReferring^^^Dr^^^SUPER-LIS~858595^DrASurname^DrReferring^^^Dr^^^GOODHOSP~2929016F^DrASurname^DrReferring^^^Dr^^^AUSHICPR ``` -------------------------------- ### Inserting and Removing Segments, Elements, and SubComponents Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Examples of inserting and removing various HL7 structural elements, including segments, components, and subcomponents. ```C# // The same Add, Insert and Remove methods are available for all items (Segments, Fields, Repeats, Components, SubComponents and Content), // Here are a few contrived examples of inserting and removing: //Segments oHL7.Insert(3, Creator.Segment("PID")); oHL7.RemoveSegmentAt(3); //Elements oHL7.Segment("PID").Element(5).Repeat(1).Insert(2, Creator.Component("PatientGivenName")); oHL7.Segment("PID").Element(5).Repeat(1).RemoveComponentAt(2); //Fields (Using Field just means the first Field/Repeat within an Element) oHL7.Segment("PID").Field(5).Insert(2, Creator.Component("PatientGivenName")); oHL7.Segment("PID").Field(5).RemoveComponentAt(2); //SubComponents oHL7.Segment("PID").Field(5).Component(15).Insert(2, Creator.SubComponent("Weird Stuff")); oHL7.Segment("PID").Field(5).Component(15).RemoveSubComponentAt(2); ``` -------------------------------- ### HL7 Escaped Data Example Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md An example of an HL7 field string containing various escape sequences. ```text |Cats\T\Dogs\~\Monkeys\S\Cows\E\Mice\.br\Shark| ``` -------------------------------- ### HasElements, HasFields, HasRepeats, HasComponents, HasSubComponents, and HasContents Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Demonstrates the boolean methods for checking the presence of structural parts in a PID segment. Includes multiple example messages and expected results. Note that HasElements and HasFields are always equal; an Element contains a list of repeating fields, and Field is always the first repeat. HasContents checks the parts between HL7 escapes (e.g., 'AUS\T\HIC' unescapes to 'AUS&HIC'). ```C# using PeterPiper.Hl7.V2.Model; // Given a message oHL7 with the example PID Segment: // PID|1||PA30000004^^^AcmeHealth^MR~22222221111^^^AUS\T\HIC^MC|123^^^NamespaceID&UniversalID&UniversalIdType|Dow^John||19850930|M //Equals: 1 // HasElements and HasFields are alway equal to each other. // The difference between an Element and a Field is what they contain. Elements contains a list of repeating fields where Field // is always the first Field repeat. So that is, oHL7.Segment("PID").Element(3).Repeat(1) is equal to oHL7.Segment("PID").Field(3) bool HasElements = oHL7.Segment("PID").HasElements; //Equals: True bool HasFields = oHL7.Segment("PID").HasFields; //Equals: True bool HasRepeats = oHL7.Segment("PID").Element(3).HasRepeats; //Equals: True bool HasComponents = oHL7.Segment("PID").Field(5).HasComponents; //Equals: True bool HasSubComponents = oHL7.Segment("PID").Field(4).Component(4).HasSubComponents; //Equals: True //Content is the parts between the HL7 escapes, so below is 1=AUS, 2=T, 3=HIC from the string 'AUS\T\HIC' which when unescaped is AUS&HIC //See Escaping for more info bool HasContents = oHL7.Segment("PID").Element(3).Repeat(2).Component(4).HasContents;//Equals: True // However, given a message oHL7 with the very simple example PID Segment of: // PID|1 bool HasElements = oHL7.Segment("PID").HasElements; //Equals: False bool HasFields = oHL7.Segment("PID").HasFields; //Equals: False // And, given a message oHL7 with the example PID Segment: // PID|1||PA30000004^^^AcmeHealth^MR|123^^^NamespaceID|Dow||19850930|M bool HasRepeats = oHL7.Segment("PID").Element(3).HasRepeats; //Equals: False bool HasComponents = oHL7.Segment("PID").Field(5).HasComponents; //Equals: False bool HasSubComponents = oHL7.Segment("PID").Field(4).Component(4).HasSubComponents; //Equals: False //And of course an empty item is also False, for example PID-100 is empty bool HasComponents = oHL7.Segment("PID").Field(100).HasComponents; //Equals: False ``` -------------------------------- ### Access MSH-7 Message creation date & time Source: https://github.com/angusmillar/peterpiper/wiki/1.-Message Shows how to get the message creation date/time from the MSH segment or via the MessageCreationDateTime property shortcut. ```C# DateTimeOffset MessageDateTime = oHL7.Segment("MSH").Field(7).Convert.DateTime.GetDateTimeOffset(); //Alternatively, you could use the MessageCreationDateTime property shortcut DateTimeOffset MessageCreationDateTime = oHL7.MessageCreationDateTime; ``` -------------------------------- ### Access MSH-12 Message Version Source: https://github.com/angusmillar/peterpiper/wiki/1.-Message Shows how to get the message version from the MSH segment or via the MessageVersion property shortcut. ```C# string MessageVersion = oHL7.Segment("MSH").Field(12).AsString; //Alternatively, you could use the MessageVersion property shortcut string MessageVersion = oHL7.MessageVersion; ``` -------------------------------- ### Parse and manipulate HL7 File with PeterPiper Creator.File Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Shows how to parse an HL7 File message with PeterPiper.Hl7.V2.Model, iterate its batches and messages, and manipulate batches (add, remove, insert, clone, clear). The example file contains two batches (BHS/BTS) each with three ORM messages; BatchCount() returns 2, FileHeader.Field(4) returns 'FileSendingFacility', and FileTrailer.Field(1) returns 2. RemoveBatchAt and InsertBatch use zero-based indices; Clone() deep-clones the entire file and ClearAll() removes all batches, FHS elements, and nulls the FTS. ```C# //Given the following File message example containing two Batches each with three HL7 Messages: //FHS|^~\\&|FileSendingApp|FileSendingFacility|ReceivingApp|ReceivingFacility|20141208064531|Security|SmithFamily|Comment|F001|Reference File Control ID //BHS|^~\\&|SendingApp|SendingFacility|ReceivingApp|ReceivingFacility|20141208064531|Security|Name/ID|Comment|B001|Reference File Control ID //MSH|^~\\&|||||20141208064525||ORM^O01^ORM_O01|0001|P|2.4 //PID|1||A000001|||Smith^John //PV1|1|I //ORC|1|000001|11111|GroupNumber1 //OBR|1|000001|11111|Test^Testing^L //MSH|^~\\&|||||20141208064526||ORM^O01^ORM_O01|0002|P|2.4 //PID|1||A000002|||Smith^Jane //PV1|1|I //ORC|1|000002|22222|GroupNumber2 //OBR|1|000002|22222|Test^Testing^L //MSH|^~\\&|||||20141208064527||ORM^O01^ORM_O01|0003|P|2.4 //PID|1||A000003|||Smith^Mary //PV1|1|I //ORC|1|000003|33333|GroupNumber3 //OBR|1|000003|33333|Test^Testing^L //BTS|BatchMessageCount|BatchComment|3 //BHS|^~\\&|SendingApp|SendingFacility|ReceivingApp|ReceivingFacility|20141208064531|Security|JonesFamily|Comment|B002|Reference File Control ID //MSH|^~\\&|||||20141208064528||ORM^O01^ORM_O01|0004|P|2.4 //PID|1||A000004|||Jones^Bob //PV1|1|I //ORC|1|000004|44444|GroupNumber4 //OBR|1|000004|44444|Test^Testing^L //MSH|^~\\&|||||20141208064529||ORM^O01^ORM_O01|0005|P|2.4 //PID|1||A000005|||Jones^Anne //PV1|1|I //ORC|1|000005|55555|GroupNumber5 //OBR|1|000005|55555|Test^Testing^L //MSH|^~\\&|||||20141208064530||ORM^O01^ORM_O01|0006|P|2.4 //PID|1||A000006|||Smith^Jill //PV1|1|I //ORC|1|000006|66666|GroupNumber6 //OBR|1|000006|66666|Test^Testing^L //BTS|BatchMessageCount|BatchComment|3 //FTS|2|BatchComment using PeterPiper.Hl7.V2.Model; string MyHL7FileMessage = "[The example File message seen above]"; IFile oFile = Creator.File(MyHL7FileMessage); int TotalBatchCount = oFile.BatchCount(); //Will equal 2 string SendingFac = oFile.FileHeader.Field(4).AsString; //Will equal 'FileSendingFacility' string FileBatchCount = oFile.FileTrailer.Field(1).AsString; //Will equal 2 //Itierate through the Batches in the File foreach (IBatch oBatch in oFile.BatchList()) { //Itierate through the HL7 mesages in the File's Batch foreach (IBatch oHL7 in oBatch.MessageList()) { string PateintFamilyName = oHL7.Segment("PID").Field(5).Component(1).AsString; } } IBatch NewBatch = Creator.Batch("[A HL7 Batch String]"); oFile.AddBatch(NewBatch); //Will add the new HL7 Batch to the end of the File's Batch list oFile.RemoveBatchAt(oFile.BatchCount()); //Will remove a batch at a give zero based index oFile.InsertBatch(NewBatch, 2); //Will insert a Batch at a give zero based index IFile ClonedFile = oFile.Clone(); //Will deep clone the entire File, and all its Batches, and their HL7 Messages, all to a new instances ClonedFile.ClearAll(); //Will remove all Batches from the File, all elements from the FHS segment and null the FTS segment ``` -------------------------------- ### Sample HL7 V2 Message Source: https://github.com/angusmillar/peterpiper/wiki/3.-Element A complete HL7 V2 message used in the code examples that follow. It contains MSH, PID, PV1, ORC, OBR, and OBX segments. ```hl7 MSH|^~\&|GOODLIS|GOODLAB|EMR|GOODHOSP|20150411102500+1000||ORU^R01^ORU_R01|300000000-1|P|2.4|||AL|NE| PID|1||1000003^^^GOOHOSP^MR~89898989^^^NationalId^NI||PatientSurname^PatientFirstname||194506241031|M|||Unit 1^111 Good Street^Brisbane^^4000^AUS^H||^PRN^PH^^^^93235615|^WPN^CP^^^^0414778341 PV1|1|O ORC|RE|300001|15100003-330001|3|CM||||201504100800+1000|||DFTR^DrBSurname^DrOrdering^^^Dr^^^GOODLIS||^WPN^PH^^^^0893412041|201504100730+1000 OBR|1|300001|15100003-330001|^^^FBE^Full Blood Count^GOODLIS|||201504100930+1000|||||||201504101100+1000||DFTR^DrBSurname^DrOrdering^^^Dr^^^GOODLIS|^WPN^PH^^^^0893412041|||||201504101115+1000||HM|F||^^^201504100800+1000^^RT|||||8003615833340784&Howser&Kara&&&Dr&&&AUSHIC OBX|1|NM|^^^HB^Haemoglobin^GOODLAB||145|g/L^^ISO+|130-180||||F OBX|2|NM|^^^RCC^Red Cell Count^GOODLAB||5.30|x10\S\12/L^^ISO+|4.50-6.50||||F OBX|3|NM|^^^HCT^HCT^GOODLAB||0.43|L/L^^ISO+|0.40-0.54||||F ``` -------------------------------- ### Access parts of a Segment in C# Source: https://github.com/angusmillar/peterpiper/wiki/2.-Segment Parses a message and retrieves a field value and a component value from the first OBX segment. The example shows how to get the result value '145' and the unit type 'ISO+'. ```csharp //Parse a whole message into the object model from a string string MessageExample = "" var oHL7 = Creator.Message(MessageExample); //TheResult variable will equal '145' string TheResult = oHL7.Segment("OBX").Field(5).AsString; //TheResultUnitType variable will equal 'ISO+' string TheResultUnitType = oHL7.Segment("OBX").Field(6).Component(3).AsString; ``` -------------------------------- ### Build and Test Commands Source: https://github.com/angusmillar/peterpiper/blob/Development/CLAUDE.md Standard CLI commands for building the solution and running tests using the .NET CLI. ```bash # Build dotnet build PeterPiper.sln dotnet build --configuration Release # Test (NuGet package is generated automatically on Release build) dotnet test TestPeterPiper/TestPeterPiper.csproj dotnet test --configuration Release # Run a single test class dotnet test --filter "ClassName=TestMessage" # Run tests in a specific category dotnet test --filter "TestCategory=Acknowledgements" ``` -------------------------------- ### Create a new Element with PeterPiper Source: https://github.com/angusmillar/peterpiper/wiki/3.-Element Shows how to create an Element instance using Creator.Element() and set its content. Includes three ways: empty then set AsString, provide content at creation, and create with sub-component content using tilde and caret separators. ```C# //Remember to include the following using statement at the top of your source file. using PeterPiper.Hl7.V2.Model; //Create a new empty element instance and then set its content var oNewElement = Creator.Element(); oNewElement.AsString = "This is an element"; //Or, provide the content as you create oNewElement = Creator.Element("This is an element"); //Or, create with some not so basic content oNewElement = Creator.Element("One~Two~ThreeOne^ThreeTwo~Four); ``` -------------------------------- ### Inspecting Content with C# Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Demonstrates creating a PID segment and iterating through its content to identify text versus escape types. ```C# using PeterPiper.Hl7.V2.Model; using PeterPiper.Hl7.V2.Support.Content; using PeterPiper.Hl7.V2.Support.Standard; //Create a PID segment ISegment oPID = Creator.Segment(@"PID|Cats\T\Dogs\T\Monkeys\T\Cows|"); //Output to the console the raw string as seen in the HL7 message Console.WriteLine($"The raw string is {oHL7.Segment("PID").Field(1).AsRawString}"); //Loop through each Content using ContentCount for (int i = 0; i < oHL7.Segment("PID").Field(1).ContentCount; i++) { //Check wether this Content if of type 'Text' or 'Escape' if (oHL7.Segment("PID").Field(1).Content(i).ContentType == ContentType.Text) { //If type 'Text' output to the console that text Console.WriteLine($"The Content at index {i.ToString()} is the text: {oHL7.Segment("PID").Field(1).Content(i).AsString}"); } //If type 'Escape' then switch on the enum of all EscapeTypes and output the name of that EscapeType else if (oHL7.Segment("PID").Field(1).Content(i).ContentType == ContentType.Escape) { Console.Write($"The Content at index {i.ToString()} is an escape of type: "); EscapeType ThisContentType = oHL7.Segment("PID").Field(1).Content(i).EscapeMetaData.EscapeType; switch (ThisContentType) { case EscapeType.Field: Console.Write(ThisContentType.ToString()); break; case EscapeType.Repeat: Console.Write(ThisContentType.ToString()); break; case EscapeType.Component: Console.Write(ThisContentType.ToString()); break; case EscapeType.SubComponent: Console.Write(ThisContentType.ToString()); break; case EscapeType.Escape: Console.Write(ThisContentType.ToString()); break; case EscapeType.HighlightOn: Console.Write(ThisContentType.ToString()); break; case EscapeType.HighlightOff: Console.Write(ThisContentType.ToString()); break; case EscapeType.LocallyDefined: Console.Write(ThisContentType.ToString()); break; case EscapeType.HexadecimalData: Console.Write(ThisContentType.ToString()); break; case EscapeType.NewLine: Console.Write(ThisContentType.ToString()); break; case EscapeType.SkipVerticalSpaces: Console.Write(ThisContentType.ToString()); break; case EscapeType.WordWrapOn: Console.Write(ThisContentType.ToString()); break; case EscapeType.WordWrapOff: Console.Write(ThisContentType.ToString()); break; case EscapeType.Indent: Console.Write(ThisContentType.ToString()); break; case EscapeType.TempIndent: Console.Write(ThisContentType.ToString()); break; case EscapeType.SkipSpacesToRight: Console.Write(ThisContentType.ToString()); break; case EscapeType.CenterNextLine: Console.Write(ThisContentType.ToString()); break; case EscapeType.Unknown: Console.Write(ThisContentType.ToString()); break; case EscapeType.NotAnEscape: Console.Write(ThisContentType.ToString()); break; default: break; } } } //Which would output to the console as follows: // The raw string is Cats\T\Dogs\~\Monkeys\S\Cows\E\Mice\.br\Shark // The Content at index 0 is the text: Cats // The Content at index 1 is an escape of type: SubComponent // The Content at index 2 is the text: Dogs // The Content at index 3 is an escape of type: Repeat // The Content at index 4 is the text: Monkeys // The Content at index 5 is an escape of type: Component // The Content at index 6 is the text: Cows // The Content at index 7 is an escape of type: Escape // The Content at index 8 is the text: Mice // The Content at index 9 is an escape of type: NewLine // The Content at index 10 is the text: Shark // As I hope you can see this is a powerful way to manipulate and manage escapes within HL7 V2 message. // We could detect strings that are highlighted or find the NewLine breaks. ``` -------------------------------- ### Parse an HL7 V2 message in C# Source: https://github.com/angusmillar/peterpiper/wiki/0.-Introduction Demonstrates how to initialize the parser with a message string and access specific components within a segment. ```C# //Simple example: using PeterPiper.Hl7.V2.Model; string MyHL7V2Message = "Any HL7 V2 Message as a string"; var oHl7 = Creator.Message(MyHL7V2Message); string sPatientSurname = oHl7.Segment("PID").Field(5).Component(1).AsString; ``` -------------------------------- ### Constructing HL7 Objects via Creator Source: https://github.com/angusmillar/peterpiper/blob/Development/CLAUDE.md Use the static Creator class to instantiate HL7 objects. Always use interfaces for variable types. ```csharp IMessage msg = Creator.Message(rawHl7String); IMessage newMsg = Creator.Message("2.4", "ADT", "A01"); // template ISegment seg = Creator.Segment("PID|..."); ``` -------------------------------- ### Access MSH-10 Message Control ID Source: https://github.com/angusmillar/peterpiper/wiki/1.-Message Shows how to get the Message Control ID from the MSH segment or via the MessageControlID property shortcut. ```C# string MessageControlID = oHL7.Segment("MSH").Field(10).AsString; //Alternatively, you could you the MessageControlID property shortcut string MessageControlID = oHL7.MessageControlID; ``` -------------------------------- ### Publishing a Release Source: https://github.com/angusmillar/peterpiper/blob/Development/CLAUDE.md Commands to trigger the automated GitHub Actions release workflow by pushing a version tag. ```bash # To publish a new release from the Release branch: git tag V10.0.0 git push origin V10.0.0 ``` -------------------------------- ### Create a new message with Creator.Message overloads Source: https://github.com/angusmillar/peterpiper/wiki/1.-Message Shows the minimum required parameters (version, message type, trigger) and optional parameters for Message Control ID and Message Structure, plus cloning and creating from an MSH segment. ```C# //The bare minimum to required start a new message Version, MessageType, MessageTrigger var oHL7 = Creator.Message("2.4", "ORU", "R01"); //Or, Assign the Message Control Id on creation of the message var oHL7 = Creator.Message("2.4", "ORU", "R01", "123456789"); //Or, Assign the Message Control Id and Message Structure on creation of the message var oHL7 = Creator.Message("2.4", "ORU", "R01", "123456789", "ORU_R01"); //Or, clone an already parsed message to create a new instance of the message string MessageExample = "" var oHL7MsgInstance1 = Creator.Message(MessageExample); var oHL7MsgInstance2 = oHL7Msg1.Clone() //Or, First create an MSH Segment instance, and then create a message from that segment instance. //Just remember you must have the minimum data of: Message Type, Message Trigger & Message Version. var MyMshSegment = Creator.Segment("MSH|^~\&|||||20160317112000+1000||ORU^R01|123456789|P|2.4|||AL|NE"); var oHL7 = Creator.Message(MyMshSegment); ``` -------------------------------- ### Access MSH-9.1 & MSH-9.2 Message type & Message Trigger Source: https://github.com/angusmillar/peterpiper/wiki/1.-Message Shows how to get the message type and trigger from the MSH segment or via the MessageType and MessageTrigger property shortcuts. ```C# string MessageType = oHL7.Segment("MSH").Field(9).Component(1).AsString; string MessageTrigger = oHL7.Segment("MSH").Field(9).Component(2).AsString; //Alternatively, you could use the MessageType & MessageTrigger property shortcut string MessageType = oHL7.MessageType; string MessageTrigger = oHL7.MessageTrigger; ``` -------------------------------- ### Manipulating HL7 Fields and Repeats Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Demonstrates creating a new field, adding it as a repeat to PID-3, removing existing repeats, and inserting a new repeat at a specific position. ```C# using PeterPiper.Hl7.V2.Model; // Given a message oHL7 with the example PID Segment: // PID|1||PA30000004^^^AcmeHealth^MR~22222221111^^^AUSHIC^MC~WA123456B^^^AUSDVA^DVG~8003608833357361^^^AUSHIC^NI||Dow^John||19850930|M //Create a new Field instance for a new identifer IField NewPyroIdentifier = Creator.Field(); NewPyroIdentifier.Component(1).AsString = "123456"; NewPyroIdentifier.Component(4).AsString = "PyroHealth"; NewPyroIdentifier.Component(5).AsString = "MR"; //Add the new Field as a repeat to the end of the list of repeats in PID-3 //PID-3 = |PA30000004^^^AcmeHealth^MR~22222221111^^^AUSHIC^MC~WA123456B^^^AUSDVA^DVG~8003608833357361^^^AUSHIC^NI~123456^^^PyroHealth^MR| oHL7.Segment("PID").Element(3).Add(NewPyroIdentifier); //Remove repeats in position two (one based index) of the list of repeats in PID-3, done twice to remove two instances //PID-3 = |PA30000004^^^AcmeHealth^MR~8003608833357361^^^AUSHIC^NI~123456^^^PyroHealth^MR| oHL7.Segment("PID").Element(3).RemoveRepeatAt(2); oHL7.Segment("PID").Element(3).RemoveRepeatAt(2); //Create another new Field instance for another identifer IField NewMillarIdentifier = Creator.Field(); NewMillarIdentifier.Component(1).AsString = "654321"; NewMillarIdentifier.Component(4).AsString = "MillarHealth"; NewMillarIdentifier.Component(5).AsString = "MR"; //Insert a repeat into the second position among the repeats in PID-3 //PID-3 = |PA30000004^^^AcmeHealth^MR~654321^^^MillarHealth^MR~8003608833357361^^^AUSHIC^NI~123456^^^PyroHealth^MR| oHL7.Segment("PID").Element(3).Insert(2, NewMillarIdentifier); ``` -------------------------------- ### Convenience Methods on IMessage Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Shows how to create an IMessage from a string and access message-level properties: MessageCreationDateTime, MessageControlID, MessageType, MessageTrigger, MessageStructure, and MessageVersion. ```C# using PeterPiper.Hl7.V2.Model; IMessage oHL7= Creator.Message("[Any HL7 V2.x message as a string]"); DateTimeOffset MessageCreationDateTime = oHL7.MessageCreationDateTime; string MessageControlID = oHL7.MessageControlID; string MessageType = oHL7.MessageType; string MessageTrigger = oHL7.MessageTrigger; string MessageStructure = oHL7.MessageStructure; string MessageVersion = oHL7.MessageVersion; ``` -------------------------------- ### Remove a segment at a given index using RemoveSegmentAt() Source: https://github.com/angusmillar/peterpiper/wiki/2.-Segment Parses an HL7 message and removes the segment at index 3 (one-based), which in the example is the PV1 segment. The method removes the segment at the specified index. ```C# //Parse a whole message into the object model from a string string MessageExample = "" var oHL7 = Creator.Message(MessageExample); //This will remove the PV1 segment from the message oHL7.RemoveSegmentAt(3); ``` -------------------------------- ### Setting Components in a PID Segment Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Sets the first and second components of field 5 (patient name) in a PID segment. Requires a message object oHL7 containing the example PID segment shown in the comment. ```C# using PeterPiper.Hl7.V2.Model; // Given a message oHL7 with the example PID Segment: // PID|1||PA30000004^^^AcmeHealth^MR~22222221111^^^AUSHIC^MC~WA123456B^^^AUSDVA^DVG~8003608833357361^^^AUSHIC^NI||Dow^John||19850930|M oHL7.Segment("PID").Field(5).Component(1).AsString = "Smith" oHL7.Segment("PID").Field(5).Component(2).AsString = "Bob"; ``` -------------------------------- ### Setting SubComponents in an OBR Segment Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Sets subcomponent values in an OBR segment: field 32 component 1 subcomponent 2 and field 5 component 1 subcomponent 3. Requires a message object oHL7 with the example OBR segment shown in the comment. ```C# using PeterPiper.Hl7.V2.Model; // Given a message oHL7 with the example OBR Segment: // OBR|1|112233|15P000005|26604007^Complete blood count^SCT||||||||||||||||||201504101115+1000||HM|F|||||||DRPRIH&DrSurname&PrincipalResultInterpreterHaem&&&DR oHL7.Segment("OBR").Field(32).Component(1).SubComponent(2).AsString = "NewSurname"; oHL7.Segment("OBR").Field(5).Component(1).SubComponent(3).AsString = "NewGivenName"; ``` -------------------------------- ### HL7 Batch Manipulation with PeterPiper Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Demonstrates how to parse a batch message, access header and trailer fields, iterate through messages, and perform CRUD operations on messages within a batch. ```C# //Given the following Batch message example containing three HL7 Messages: //BHS|^~\&|SendingApp|SendingFacility|ReceivingApp|ReceivingFacility|20141208064531|Security|Name/ID|Comment|Control ID|Reference File Control ID //MSH|^~\&|||||20141208064525||ORM^O01^ORM_O01|0001|P|2.4 //PID|1||A000001|||Smith^John //PV1|1|I //ORC|1|000001|11111|GroupNumber1 //OBR|1|000001|11111|Test^Testing^L //MSH|^~\&|||||20141208064526||ORM^O01^ORM_O01|0002|P|2.4 //PID|1||A000002|||Smith^Jane //PV1|1|I //ORC|1|000002|22222|GroupNumber2 //OBR|1|000002|22222|Test^Testing^L //MSH|^~\&|||||20141208064527||ORM^O01^ORM_O01|0003|P|2.4 //PID|1||A000003|||Smith^Mary //PV1|1|I //ORC|1|000003|33333|GroupNumber3 //OBR|1|000003|33333|Test^Testing^L //BTS|BatchMessageCount|BatchComment|BatchTotals using PeterPiper.Hl7.V2.Model; string MyHL7BatchMessage = "[The example Batch message seen above]"; IBatch oBatch = Creator.Batch(MyHL7BatchMessage); int TotalMessageCount = oBatch.MessageCount(); //Will equal 3 string SendingFac = oBatch.BatchHeader.Field(4).AsString; //Will equal SendingFacility string BatchMsgCount = oBatch.BatchTrailer.Field(1).AsString; //Will equal BatchMessageCount //Itierate through the messges in the Batch foreach (IMessage oHL7 in oBatch.MessageList()) { string PateintFamilyName = oHL7.Segment("PID").Field(5).Component(1).AsString; } IMessage NewHL7Message = Creator.Message("[A HL7 Message String]"); oBatch.AddMessage(NewHL7Message); //Will add the HL7 mesage to the end of the Batch's Message list oBatch.RemoveMessageAt(oBatch.MessageCount()); //Will remove a message at a give zero based index oBatch.InsertMessage(NewHL7Message, 2); //Will insert a message at a give zero based index IBatch ClonedBatch = oBatch.Clone(); //Will deep clone the entire Batch, and all its messages, all to a new instances ClonedBatch.ClearAll(); //Will remove all messages from ther batch, all elements from the BHS segment and null the BTS segment ``` -------------------------------- ### Create a new OBX segment in C# Source: https://github.com/angusmillar/peterpiper/wiki/2.-Segment Shows two ways to create an OBX segment: by building it field by field, or by parsing a raw string. Requires the using statement for PeterPiper.Hl7.V2.Model. ```csharp //Remember to include the following using statement at the top of you source file. using PeterPiper.Hl7.V2.Model; //Create a new OBX segment instance as follows: //example: OBX|1|NM|^^^HB^Haemoglobin^GOODLAB||145|g/L^^ISO+|130-180||||F|||201504100930+1000 var oNewOBXSegment = Creator.Segment("OBX"); oNewOBXSegment.Field(1).AsString = "1"; oNewOBXSegment.Field(2).AsString = "NM"; oNewOBXSegment.Field(3).Component(4).AsString = "HB"; oNewOBXSegment.Field(3).Component(5).AsString = "Haemoglobin"; oNewOBXSegment.Field(3).Component(6).AsString = "GOODLAB"; oNewOBXSegment.Field(5).AsString = "145"; oNewOBXSegment.Field(6).Component(1).AsString = "g/L"; oNewOBXSegment.Field(6).Component(3).AsString = "ISO+"; oNewOBXSegment.Field(7).AsString = "130-180"; oNewOBXSegment.Field(11).AsString = "F"; oNewOBXSegment.Field(14).AsString = "201504100930+1000"; //Or, even just create it by parsing the whole segment as a raw string, like this var oNewOBXSegment = Creator.Segment("OBX|1|NM|^^^HB^Haemoglobin^GOODLAB||145|g/L^^ISO+|130-180||||F|||201504100930+1000"); ``` -------------------------------- ### Configure and use custom HL7 V2 escape sequences Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Demonstrates how to define custom delimiters using IMessageDelimiters and apply them when creating messages and segments. Note that using custom sequences is discouraged as few systems support them. ```C# using PeterPiper.Hl7.V2.Model; using PeterPiper.Hl7.V2.Support.Tools; // It is legal in HL7 V2.x to use completely different escape sequences in the HL7 V2 message. // This is instead of the typical escape sequences of '|^~\&'. // To do so you would set the sequences you wish to use in the MSH-1 and MSH-2 Fields. // PeterPiper can also handle these custom escape sequences characters as per the examples below. // However, I highly advise you avoid this behavior as very few systems can coupe with it. //My new custom escape sequences characters char FieldChar = '!'; char RepeatChar = '%'; char ComponentChar = '*'; char SubComponentChar = '@'; char EscapeChar = '#'; IMessageDelimiters MessageDelimiters = Creator.MessageDelimiters(FieldChar, RepeatChar, ComponentChar, SubComponentChar, EscapeChar); // A message instance using my custom escape sequences IMessage oHL7 = Creator.Message("MSH!*%#@!SendApp!SendFacility!RecApp!RecFacility!20140527095657!!ORU*R01!0000000000000000010D!P!2.3.1"); // A new Segment also using the same custom escape sequences // Notice that when using custom escape sequences you must always provide the IMessageDelimiters object // when creating new segments. ISegment oSeg = Creator.Segment("PID!!!!!Dow*John!!19850930!M", MessageDelimiters); oHL7.Add(oSeg); //Given an already parsed message you can the IMessageDelimiters that is it currently using as follows: IMessageDelimiters ThisMessagesDelimiters = target.MessageDelimiters; //As I said earlier, I doubt you will every use this functionality in the real world. ``` -------------------------------- ### Locate a Segment in C# Source: https://github.com/angusmillar/peterpiper/wiki/2.-Segment Shows multiple ways to find a specific OBX segment when there are many: using lambda expressions, one-based index, zero-based index in a list, and iterating all segments. The lambda approach is recommended as the most flexible. ```csharp //Parse a whole message into the object model from a string string MessageExample = "" var oHL7 = Creator.Message(MessageExample); //Setup some string variables to populate. //We will get the Haemoglobin's result value and the HCT's normal range. string TheHaemoglobinResultValue = string.Empty; string TheHctNormalRangeValue = string.Empty; //Check there are more then zero OBX Segments in our message if (oHL7.SegmentCount("OBX") > 0) { //Obtain a reference to the segments we are interested in using a lambda expression var oOBXHemaglobin = oHL7.SegmentList("OBX").SingleOrDefault(x => x.Field(3).Component(4).AsString == "HB"); var oOBXHct = oHL7.SegmentList("OBX").SingleOrDefault(x => x.Field(3).Component(4).AsString == "HCT"); //Get the values we are interested in from the segment instances we collected above. //I also check we actually found the segment with a check for null. if (oOBXHemaglobin != null) TheHaemoglobinResultValue = oOBXHemaglobin.Field(5).AsString; if (oOBXHct != null) TheHctNormalRangeValue = oOBXHct.Field(7).AsString; //Although not as useful we can also locate a segment via its one base index in the message //This gets the sixth segment oOBXHemaglobin = oHL7.Segment(6); //This gets the egith segment oOBXHct = oHL7.Segment(8); //Or we could get the list of all the OBX segments and locate via the zero based index in the list oOBXHemaglobin = oHL7.SegmentList("OBX")[0]; oOBXHct = oHL7.SegmentList("OBX")[2] //Or finally, you could iterate all segments looking for what you want //Take note that this is a one based index so i = 1 for the loop for (int i = 1; i < oHL7.SegmentCount(); i++) { if (oHL7.Segment(i).Code == "OBX") { if (oHL7.Segment(i).Field(3).Component(4).AsString == "HB") { TheHaemoglobinResultValue = oHL7.Segment(i).Field(5).AsString; } } } //Although, of all these, I find the first example using the lambda expression the most flexible and useful } ``` -------------------------------- ### Check HL7 Null and Empty Fields in C# Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Demonstrates how to use the IsEmpty and IsHL7Null properties on an OBR segment field to determine if a field is empty or explicitly set to HL7 Null. ```C# using PeterPiper.Hl7.V2.Model; // Given a message oHL7 with the example OBR Segment: // OBR|1||""|Rhubarb^SerumRhubarb^L||||||||||||||||||201504101115+1000||HM|F // IsEmpty = False bool IsEmpty = oHL7.Segment("OBR").Field(1).IsEmpty; // IsEmpty = True bool IsEmpty = oHL7.Segment("OBR").Field(2).IsEmpty; // IsEmpty = False bool IsEmpty = oHL7.Segment("OBR").Field(3).IsEmpty; // IsHL7Null = False bool IsHL7Null = oHL7.Segment("OBR").Field(1).IsHL7Null; // IsHL7Null = False bool IsHL7Null = oHL7.Segment("OBR").Field(2).IsHL7Null; // IsHL7Null = True bool IsHL7Null = oHL7.Segment("OBR").Field(3).IsHL7Null; ``` -------------------------------- ### Create a new HL7 message Source: https://github.com/angusmillar/peterpiper/blob/Development/README.md Shows two ways to initialize a new message: using version, type, and trigger parameters, or by passing a raw MSH segment string. ```C# using PeterPiper.Hl7.V2.Model; string MessageVersion = "2.4"; string MessageType = "ADT"; string MessageTrigger = "A01"; IMessage oHL7 = Creator.Message(MessageVersion, MessageType, MessageTrigger); //Or IMessage oHL7 = Creator.Message(@"MSH|^~\&|||||||ADT^A01|||2.4|"); //Output the finished message: string FinalMessage = oHL7.AsRawString; ```