CdaLib 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CdaLib --version 1.2.0
                    
NuGet\Install-Package CdaLib -Version 1.2.0
                    
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="CdaLib" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CdaLib" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="CdaLib" />
                    
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 CdaLib --version 1.2.0
                    
#r "nuget: CdaLib, 1.2.0"
                    
#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 CdaLib@1.2.0
                    
#: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=CdaLib&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=CdaLib&version=1.2.0
                    
Install as a Cake Tool

CdaLib

A .NET library for parsing and generating C-CDA (Consolidated Clinical Document Architecture) documents with full ONC 170.315(b)(1) Transitions of Care compliance.

๐Ÿš€ Features

  • โœ… Parse C-CDA XML documents into domain models
  • โœ… Generate C-CDA XML documents from domain models
  • โœ… FHIR Bundle Support - Convert FHIR R4 bundles to C-CDA documents โญ NEW
  • โœ… Render C-CDA documents as beautiful, responsive HTML reports
  • โœ… XML to HTML - Direct conversion from C-CDA XML to HTML โญ NEW
  • โœ… Validate documents against XSD schemas and ONC conformance requirements
  • โœ… Multi-Document Type Support - CCD, Discharge Summary, Referral Note, and more
  • โœ… ONC Certification Ready - Full sender/receiver conformance validation
  • โœ… C-CDA 2.1 & USCDI V3 Compliant

๐Ÿ“‹ Quick Start

Installation

Core Library:

dotnet add package CdaLib --version 1.1.0

HTML Rendering (Optional):

dotnet add package CdaLib.Rendering --version 1.1.0

Or via Package Manager:

Install-Package CdaLib -Version 1.1.0
Install-Package CdaLib.Rendering -Version 1.1.0

Basic Usage

using CdaLib;
using CdaLib.Parsing;
using CdaLib.Domain;

// Create a service for Continuity of Care Document
var service = new CdaService(CcdaDocumentTypes.ContinuityOfCareDocument);

// Create your clinical data
var ccd = new Ccd
{
    Patient = new Patient { FirstName = "John", LastName = "Doe", /* ... */ },
    Problems = new List<Problem> { /* ... */ },
    Medications = new List<Medication> { /* ... */ },
    Allergies = new List<Allergy> { /* ... */ }
};

// Generate C-CDA XML with sender conformance validation
var (errors, xmlDoc) = service.Export(ccd);
if (errors.Any())
{
    // Handle validation errors
    foreach (var error in errors)
    {
        Console.WriteLine($"{error.ProblemType}: {error.Reason}");
    }
}

// Save to file
service.ExportToFile(ccd, "output.xml");

// Parse an existing C-CDA document with receiver conformance validation
var (importErrors, parsedCcd) = service.Import("input.xml");

// NEW: Render as responsive HTML medical report
var renderer = new CdaLib.Rendering.CdaHtmlRenderer(CcdaDocumentTypes.ContinuityOfCareDocument);
renderer.RenderToFile(parsedCcd, "patient-report.html");

// NEW: Convert FHIR Bundle to C-CDA
var fhirBundle = GetFhirBundle(); // Your FHIR R4 Bundle
var (fhirErrors, fhirXml) = service.ExportFromFhirBundle(fhirBundle);

// NEW: Convert C-CDA XML directly to HTML
var htmlRenderer = new CdaLib.Rendering.CdaHtmlRenderer(CcdaDocumentTypes.ContinuityOfCareDocument);
var html = htmlRenderer.RenderXmlToHtml(xmlDoc.ToString());

๐Ÿ“š Documentation

Getting Started

ONC Certification & Compliance

Document Type Implementations

HTML Rendering

FHIR Integration

๐Ÿฅ Supported Document Types

Document Type LOINC Code Template ID ONC ToC Compatible
Continuity of Care Document 34133-9 2.16.840.1.113883.10.20.22.1.2 โœ…
Discharge Summary 18842-5 2.16.840.1.113883.10.20.22.1.1 โœ…
Referral Note 57133-1 2.16.840.1.113883.10.20.22.1.14 โœ…
Transfer Summary 18776-5 2.16.840.1.113883.10.20.22.1.1 โœ…
History and Physical 11488-4 2.16.840.1.113883.10.20.22.1.1 โœ…
Progress Note 11506-3 2.16.840.1.113883.10.20.22.1.1 โš ๏ธ
Consultation Note 11488-4 2.16.840.1.113883.10.20.22.1.1 โš ๏ธ
Procedure Note 28570-0 2.16.840.1.113883.10.20.22.1.1 โš ๏ธ
Operative Note 11506-3 2.16.840.1.113883.10.20.22.1.1 โš ๏ธ
Emergency Department Note 11488-4 2.16.840.1.113883.10.20.22.1.1 โš ๏ธ

๐Ÿ” Validation System

Conformance Modes

CdaLib includes a sophisticated validation system with two conformance modes based on ONC Direct Protocol requirements:

Sender Conformance (ยง 170.202(d)) - Strict

For generating documents to send to other systems:

  • Validates all required fields
  • Checks document-type-specific required sections (Problems, Medications, Allergies for CCD)
  • Ensures proper template IDs and document structure
  • Validates header elements (patient, author, custodian)
  • Used automatically in service.Export()
Receiver Conformance (ยง 170.202(a)(2)) - Lenient

For parsing documents received from other systems:

  • More tolerant of missing optional fields
  • Focuses on structural integrity
  • Allows parsing even with minor issues
  • Tracks validation issues without rejecting documents
  • Used automatically in service.Import()

Example: Using Validation

using CdaLib;
using CdaLib.Validation;
using CdaLib.Parsing;

// Export with sender validation (strict)
var service = new CdaService(CcdaDocumentTypes.ContinuityOfCareDocument);
var (exportErrors, xml) = service.Export(ccd);
if (exportErrors.Any())
{
    Console.WriteLine("Document does not meet sender requirements:");
    foreach (var error in exportErrors)
    {
        Console.WriteLine($"  - [{error.ProblemType}] {error.Reason}");
    }
}

// Import with receiver validation (lenient)
var (importErrors, parsedCcd) = service.Import("received-document.xml");
// Parser will extract data even if there are minor conformance issues
Console.WriteLine($"Parsed patient: {parsedCcd.Patient.FirstName} {parsedCcd.Patient.LastName}");

// Advanced: Use validator directly for custom validation
var conformanceErrors = CdaValidator.ValidateWithConformance(
    xml,
    ConformanceMode.Sender,
    CcdaDocumentTypes.ContinuityOfCareDocument);

๐Ÿงช Testing Against ONC Validator

Sender Testing (Generate Documents)

  1. Generate a C-CDA document:
var service = new CdaService(CcdaDocumentTypes.ContinuityOfCareDocument);
var (errors, xml) = service.Export(myCcdData);
xml.Save("my-document.xml");
  1. Upload my-document.xml to https://site.healthit.gov/c-cda/uscdi-v3
  2. Select Sender validation criteria
  3. Choose 170.315_b1_ToC_Amb scenario
  4. Verify all IG conformance and S&CC Reference validation passes

Receiver Testing (Parse Documents)

  1. Download scenario XML files from the ONC validator
  2. Parse them with CdaLib:
var service = new CdaService(CcdaDocumentTypes.ContinuityOfCareDocument);
var (errors, ccd) = service.Import("scenario-file.xml");
  1. Verify data extraction is successful
  2. Upload scenario files to validator with Receiver criteria

๐Ÿ“ฆ Domain Model

The library uses a rich domain model for representing clinical data:

Core Components

  • Ccd - Main document container
  • Patient - Patient demographics and information
  • Author - Document author information
  • Custodian - Document custodian (organization)

Clinical Data

  • Problems - Conditions and diagnoses
  • Medications - Medication activities and prescriptions
  • Allergies - Allergies and intolerances
  • VitalSigns - Vital sign measurements
  • Procedures - Procedures and interventions โญ NEW
  • Immunizations - Vaccination history โญ NEW
  • Results - Laboratory results
  • Encounters - Healthcare encounters โญ NEW
  • Goals - Patient goals โญ NEW
  • SocialHistory - Social history including smoking status

USCDI V3 Extensions

  • Goals - Patient goals
  • HealthConcerns - Health concerns
  • SDOH - Social Determinants of Health
  • Notes - Clinical notes
  • UDIs - Unique Device Identifiers
  • Occupation - Occupational data
  • TribalAffiliation - Tribal affiliation
  • PregnancyObservations - Pregnancy status
  • DisabilityStatus - Disability observations
  • FunctionalStatus - Functional status
  • Payer - Insurance payer information
  • And more...

๐Ÿ”ง Advanced Features

FHIR Bundle to C-CDA Conversion

Convert FHIR R4 bundles directly to C-CDA documents:

using CdaLib;
using Hl7.Fhir.Model; // Microsoft FHIR SDK

// Create a FHIR Bundle with patient data
var bundle = new Bundle
{
    Entry = new List<Bundle.EntryComponent>
    {
        new Bundle.EntryComponent { Resource = new Patient { /* ... */ } },
        new Bundle.EntryComponent { Resource = new AllergyIntolerance { /* ... */ } },
        new Bundle.EntryComponent { Resource = new Condition { /* ... */ } },
        new Bundle.EntryComponent { Resource = new MedicationStatement { /* ... */ } },
        new Bundle.EntryComponent { Resource = new Procedure { /* ... */ } },
        new Bundle.EntryComponent { Resource = new Immunization { /* ... */ } },
        new Bundle.EntryComponent { Resource = new Encounter { /* ... */ } },
        new Bundle.EntryComponent { Resource = new Observation { /* ... */ } },
        new Bundle.EntryComponent { Resource = new DiagnosticReport { /* ... */ } },
        new Bundle.EntryComponent { Resource = new Goal { /* ... */ } }
    }
};

// Convert to C-CDA
var service = new CdaService(CcdaDocumentTypes.ContinuityOfCareDocument);
var (errors, xmlDoc) = service.ExportFromFhirBundle(bundle);

if (!errors.Any())
{
    xmlDoc.Save("fhir-to-ccda.xml");
}

Supported FHIR Resources:

  • โœ… Patient
  • โœ… AllergyIntolerance
  • โœ… Condition
  • โœ… MedicationStatement / MedicationRequest
  • โœ… Procedure โญ NEW
  • โœ… Immunization โญ NEW
  • โœ… Encounter โญ NEW
  • โœ… Observation (Results & Vital Signs) โญ NEW
  • โœ… DiagnosticReport โญ NEW
  • โœ… Goal โญ NEW
  • โœ… Practitioner
  • โœ… Organization

XML to HTML Rendering

Convert C-CDA XML directly to HTML without parsing:

using CdaLib.Rendering;

// From XML string
var xmlString = File.ReadAllText("document.xml");
var renderer = new CdaHtmlRenderer(CcdaDocumentTypes.ContinuityOfCareDocument);
var html = renderer.RenderXmlToHtml(xmlString);
File.WriteAllText("output.html", html);

// From XDocument
var xdoc = XDocument.Load("document.xml");
var html2 = renderer.RenderXmlToHtml(xdoc);

// ONC Style renderer also supports XML input
var oncRenderer = new OncStyleHtmlRenderer(CcdaDocumentTypes.ContinuityOfCareDocument);
var oncHtml = oncRenderer.RenderXmlToHtml(xmlString);

Multiple Document Types

Generate different document types for different use cases:

// Discharge Summary
var dischargeService = new CdaService(CcdaDocumentTypes.DischargeSummary);
var (errors1, dischargeXml) = dischargeService.Export(ccd);

// Referral Note
var referralService = new CdaService(CcdaDocumentTypes.ReferralNote);
var (errors2, referralXml) = referralService.Export(ccd);

// Transfer Summary
var transferService = new CdaService(CcdaDocumentTypes.TransferSummary);
var (errors3, transferXml) = transferService.Export(ccd);

Backward Compatibility

Static helper methods are available for quick operations:

// Quick compile without service
var xml = CcdCompiler.CompileStatic(ccd, CcdaDocumentTypes.ContinuityOfCareDocument);

// Quick parse without service
var parsedCcd = CcdaParser.ParseStatic(xml, CcdaDocumentTypes.ContinuityOfCareDocument);

Custom Validation

Use the validator directly for more control:

using CdaLib.Validation;

// XSD validation only
var xsdErrors = CdaValidator.Validate(xmlPath, ValidationMode.Strict);

// Full conformance validation
var allErrors = CdaValidator.ValidateWithConformance(
    xmlDoc,
    ConformanceMode.Sender,
    CcdaDocumentTypes.ContinuityOfCareDocument
);

๐Ÿ—๏ธ Architecture

Key Components

  • CdaService - Primary user-facing API
  • CcdCompiler - Compiles domain models to C-CDA XML
  • CcdaParser - Parses C-CDA XML to domain models
  • CdaValidator - XSD and conformance validation
  • ConformanceValidator - ONC sender/receiver conformance
  • CcdaDocumentTypes - Document type definitions

Design Principles

  1. Type Safety - Document types are strongly typed
  2. Explicit Configuration - No hidden defaults, clear intent
  3. Separation of Concerns - Parsing, compilation, and validation are distinct
  4. Standards Compliance - Follows C-CDA 2.1 and ONC requirements
  5. Flexibility - Support for both instance-based and static usage

๐Ÿงช Testing

The library includes comprehensive tests:

# Run all tests
dotnet test

# Run specific test categories
dotnet test --filter "FullyQualifiedName~OncTransitionOfCareTests"
dotnet test --filter "FullyQualifiedName~CcdCompilerTests"
dotnet test --filter "FullyQualifiedName~CdaServiceTests"

Test Categories

  • OncTransitionOfCareTests - ONC 170.315(b)(1) scenario tests
  • CcdCompilerTests - Compiler functionality and roundtrip tests
  • CcdaParserTests - Parser functionality tests
  • CdaServiceTests - Service workflow tests
  • DocumentTypeTests - Document type configuration tests
  • SenderConformanceTests - Sender conformance validation
  • ReceiverConformanceTests - Receiver conformance validation

๐Ÿ“– Additional Resources

Standards References

Online Tools

๐Ÿค Contributing

When contributing, ensure:

  1. All tests pass (dotnet test)
  2. Code follows existing patterns
  3. Document any new features
  4. Validate against ONC online validator

๐Ÿ“„ License

[Add your license information here]

๐Ÿ†˜ Support

For issues or questions:

  1. Check the documentation guides above
  2. Review test examples in CdaLib.Tests/
  3. Validate with the ONC online validator
  4. Open an issue with specific error messages

Version: 1.1.0
Target Framework: .NET 8.0, .NET 9.0
C-CDA Version: 2.1
USCDI Version: V3
ONC Certification: 170.315(b)(1) Transitions of Care
NuGet Packages:

  • CdaLib (1.1.0) - Core library
  • CdaLib.Rendering (1.1.0) - HTML rendering (depends on CdaLib 1.1.0)
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 is compatible.  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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CdaLib:

Package Downloads
CdaLib.Rendering

HTML rendering helpers for CdaLib, including ONC-style validator HTML and a modern Bootstrap-based renderer for C-CDA documents.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.21.0 117 2/27/2026
1.20.0 116 2/24/2026
1.19.0 115 2/23/2026
1.18.0 147 2/9/2026
1.17.0 122 2/3/2026
1.16.0 126 1/30/2026
1.15.0 118 1/27/2026
1.14.0 120 1/21/2026
1.13.0 114 1/21/2026
1.12.0 491 1/20/2026
1.11.0 123 1/18/2026
1.10.0 125 1/15/2026
1.9.0 129 1/14/2026
1.8.0 118 1/14/2026
1.7.0 115 1/14/2026
1.6.0 120 1/13/2026
1.5.0 121 1/13/2026
1.4.0 129 1/8/2026
1.3.0 123 1/7/2026
1.2.0 119 1/7/2026
Loading failed

See CHANGELOG.md for release notes