LSchuster.HL7MessageProcessor 1.0.7

dotnet add package LSchuster.HL7MessageProcessor --version 1.0.7
                    
NuGet\Install-Package LSchuster.HL7MessageProcessor -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LSchuster.HL7MessageProcessor" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LSchuster.HL7MessageProcessor" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="LSchuster.HL7MessageProcessor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LSchuster.HL7MessageProcessor --version 1.0.7
                    
#r "nuget: LSchuster.HL7MessageProcessor, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LSchuster.HL7MessageProcessor@1.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LSchuster.HL7MessageProcessor&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=LSchuster.HL7MessageProcessor&version=1.0.7
                    
Install as a Cake Tool

HL7 .Net Message Processor

LSCH.HL7MessageProcessor is a .NET library for parsing and working with HL7 v2.x messages. It provides structured access to message headers and values without mutating the input string, making it suitable for read-only inspection, validation, and routing scenarios.

Features

  • ๐Ÿ” Deep field access by segment name and component indices
  • ๐Ÿงฉ Immutable HL7ReadonlyMessage wrapper
  • ๐Ÿ” Mutable transformations with HL7TransformableMessage for editing HL7 content
  • โœ… HL7-compliant header parsing (MSH segment)
  • ๐Ÿงพ Automatic ACK generation (CA, AE, AR, CR, CE) with error message support
  • ๐Ÿงช Tested with xUnit and FluentAssertions
  • โš™๏ธ Optimized for performance with ReadOnlySpan<char>

Install via NuGet

dotnet add package LSchuster.HL7MessageProcessor

Or via NuGet Package Manager:

Install-Package LSchuster.HL7MessageProcessor

๐Ÿš€ Quick Start

using LSCH.HL7MessageProcessor;

// Load an HL7 message
var rawMessage = File.ReadAllText("some-message.hl7");

// Create a readonly HL7 message wrapper
var hl7 = new HL7ReadonlyMessage(rawMessage);

// โœ… Access message header fields
var sendingApp = hl7.Header.SendingApplication;
var messageDate = hl7.Header.ParsedDateTime;

// โœ… Extract a single field value (e.g., PID-3.1)
string patientId = hl7.GetValue("PID", 3, 1);

// โœ… Extract values across repeated segments (e.g., OBR-4.2 from all OBRs)
IReadOnlyList<string> values = hl7.GetValues("OBR", 4, 2);

// โœ… Use HL7 field path notation (e.g., "OBR-4.2", "PID-5.1.2")
string id = hl7.GetValue("PID-3.1");
IReadOnlyList<string> lipidTestCodes = hl7.GetValues("OBR-4.1");

// โœ… Validate structural correctness
bool isValid = hl7.IsValid();

// โœ… Detect if the message is an HL7 acknowledgment
bool isAck = hl7.IsAcknowledgment();
Console.WriteLine($"Is ACK? {isAck}");

// โœ… Generate an ACK in response
if (!isAck && isValid)
{
    var ack = hl7.GenerateAcknowledgement(
        switchSenderAndReceiver: true,
        acknowledgmentCode: "AE", // "CA" = Accept, "AE" = App Error, "AR" = App Reject, etc.
        errorMessage: "Missing required field: PID-5"
    );

    string ackRaw = ack.ToString();
    Console.WriteLine("Generated ACK:\n" + ackRaw);
}

โœ๏ธ Modify the HL7 Message (Transformable)

// Use HL7TransformableMessage for write access
var transformable = new HL7TransformableMessage(rawMessage);

// Redact patient name (e.g., PID-5)
transformable.SetValue("PID", "REDACTED", 5);

// Remove all NTE segments
transformable.RemoveSegments("NTE");

// Add a custom Z-segment
transformable.AddSegment("ZBX", "Custom|Value|Here");

// Get the updated HL7 message
string updatedMessage = transformable.ToString();
File.WriteAllText("transformed.hl7", updatedMessage);

Testing

The library includes a dedicated xUnit test project that uses real HL7 sample messages. Tests verify structural parsing, header field correctness, and field access methods.

Run tests locally:

dotnet test

Project Structure

src/
  HL7MessageProcessor/             # Library project
test/
  HL7MessageProcessor.Tests/      # xUnit test project
  โ””โ”€โ”€ TestData/                   # Contains sample .hl7 files

License

This project is licensed under the MIT License.

Author

GitHub @LSchuster

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 227 5/7/2025
1.0.6 210 5/7/2025
1.0.5 197 5/6/2025
1.0.4 202 5/6/2025
1.0.3 199 5/6/2025
1.0.2 205 5/4/2025
1.0.1 126 5/3/2025
1.0.0 128 5/3/2025