Uk.Legislation 10.0.6

dotnet add package Uk.Legislation --version 10.0.6
                    
NuGet\Install-Package Uk.Legislation -Version 10.0.6
                    
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="Uk.Legislation" Version="10.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Uk.Legislation" Version="10.0.6" />
                    
Directory.Packages.props
<PackageReference Include="Uk.Legislation" />
                    
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 Uk.Legislation --version 10.0.6
                    
#r "nuget: Uk.Legislation, 10.0.6"
                    
#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 Uk.Legislation@10.0.6
                    
#: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=Uk.Legislation&version=10.0.6
                    
Install as a Cake Addin
#tool nuget:?package=Uk.Legislation&version=10.0.6
                    
Install as a Cake Tool

Uk.Legislation

A comprehensive .NET client library for the UK Government's Legislation API (legislation.gov.uk).

NuGet License: MIT

Features

  • ๐ŸŽฏ Type Safe - Strongly-typed enum-based API using LegislationType
  • ๐Ÿ”’ 100% API Coverage - Support for all legislation.gov.uk XML/Atom endpoints
  • ๐Ÿ”„ Resilient - Built-in retry and circuit breaker policies with Polly
  • ๐Ÿ“ฆ DI Ready - First-class dependency injection support
  • ๐Ÿงช Well Tested - 26 unit and integration tests, all passing
  • ๐Ÿ“– Documented - Full XML documentation on all public APIs
  • ๐ŸŒ Live API Verified - Tested against real legislation.gov.uk

Installation

Install via NuGet Package Manager:

Install-Package Uk.Legislation

Or via .NET CLI:

dotnet add package Uk.Legislation

Quick Start

Basic Usage (Type-Safe with Enums)

using Uk.Legislation;
using Uk.Legislation.Extensions;
using Uk.Legislation.Models.Common;

// Create a client
var client = new LegislationClient();

// Get the Human Rights Act 1998 (type-safe!)
var act = await client.Legislation.GetLegislationAsync(
    LegislationType.UkPublicGeneralAct,
    1998,
    42);

Console.WriteLine($"Title: {act.Title}");
Console.WriteLine($"Year: {act.Year}");

// Get raw XML
var xml = await client.Legislation.GetLegislationXmlAsync(
    LegislationType.UkPublicGeneralAct,
    1998,
    42);

// Get point-in-time version
var historicalXml = await client.Legislation.GetLegislationAtDateXmlAsync(
    LegislationType.UkPublicGeneralAct,
    1998,
    42,
    "2020-01-01");

// Get as-enacted version
var enactedXml = await client.Legislation.GetLegislationAsEnactedXmlAsync(
    LegislationType.UkPublicGeneralAct,
    1998,
    42);

// Browse all UK Acts from 1998
var pagedResults = await client.Legislation.GetLegislationByTypeAndYearAsync(
    LegislationType.UkPublicGeneralAct,
    1998);

foreach (var item in pagedResults.Results)
{
    Console.WriteLine($"{item.Number}. {item.Title}");
}

Dependency Injection

using Uk.Legislation.Extensions;

// In your Startup.cs or Program.cs
services.AddUkLegislationClient(options =>
{
    options.Timeout = TimeSpan.FromSeconds(30);
    options.UserAgent = "MyApp/1.0";
});

// Or with resilience policies (recommended for production)
services.AddUkLegislationClientWithResilience(options =>
{
    options.MaxRetryAttempts = 3;
});

Using in Your Service

using Uk.Legislation;
using Uk.Legislation.Extensions;
using Uk.Legislation.Models.Common;

public class LegislationService
{
    private readonly LegislationClient _client;

    public LegislationService(LegislationClient client)
    {
        _client = client;
    }

    public async Task<string> GetActTitleAsync(int year, int number)
    {
        var act = await _client.Legislation.GetLegislationAsync(
            LegislationType.UkPublicGeneralAct,
            year,
            number);
        
        return act.Title;
    }
}

Supported Legislation Types

All 37 UK legislation types are supported via the LegislationType enum:

Primary Legislation

  • UK Public General Acts (LegislationType.UkPublicGeneralAct) - "ukpga"
  • UK Local Acts (LegislationType.UkLocalAct) - "ukla"
  • UK Private Acts (LegislationType.UkPrivateAct) - "ukppa"
  • Scottish Acts (LegislationType.ScottishAct) - "asp"
  • Welsh Acts (LegislationType.SeneddAct) - "asc"
  • Church Measures (LegislationType.ChurchMeasure) - "ukcm"
  • Northern Ireland Acts (LegislationType.NorthernIrelandAct) - "nia"

Secondary Legislation

  • UK Statutory Instruments (LegislationType.UkStatutoryInstrument) - "uksi"
  • Scottish Statutory Instruments (LegislationType.ScottishStatutoryInstrument) - "ssi"
  • Welsh Statutory Instruments (LegislationType.WalesStatutoryInstrument) - "wsi"
  • Northern Ireland Statutory Rules (LegislationType.NorthernIrelandStatutoryRule) - "nisr"
  • Church Instruments (LegislationType.ChurchInstrument) - "ukci"

EU Legislation

  • EU Regulations (LegislationType.EuRegulation) - "eur"
  • EU Directives (LegislationType.EuDirective) - "eudr"
  • EU Decisions (LegislationType.EuDecision) - "eudn"

And 22 more types! See LegislationType enum for the complete list.

Working Features

โœ… Core Legislation Retrieval

  • Get legislation by type, year, and number
  • Get legislation as XML (CLML format)
  • Get legislation as HTML
  • Get table of contents
  • Point-in-time versions (historical snapshots)
  • As-enacted versions (original text)

โœ… Atom Feeds & Browsing

  • Browse by legislation type
  • Browse by type and year
  • Automatic pagination handling
  • Parse Atom feeds to typed models

โœ… Type Safety

  • Enum-based legislation types
  • No string literals needed
  • IntelliSense support
  • Compile-time validation

API Coverage Status

Phase 1 โœ… - Foundation

  • Core project structure
  • Configuration options
  • Dependency injection support
  • Exception handling

Phase 2 โœ… - Core Legislation API

  • XML/Atom endpoint integration
  • Type-safe enum-based API
  • Point-in-time versions
  • Atom feed parsing
  • 26 tests (all passing)

Phase 3 โณ - Enhanced XML Parsing

  • Full CLML parsing
  • Extract all metadata
  • Parse document structure

Phase 4+ โณ - Planned

  • Search API
  • Changes & amendments tracking
  • Effects & impacts
  • Advanced caching

See MASTER_PLAN.md for the complete roadmap.

Configuration Options

var options = new LegislationClientOptions
{
    BaseUrl = "https://www.legislation.gov.uk",
    Timeout = TimeSpan.FromSeconds(30),
    UserAgent = "MyApp/1.0",
    MaxRetryAttempts = 3
};

var client = new LegislationClient(options);

Type Conversion Utilities

Convert between enum and URI codes:

using Uk.Legislation.Extensions;
using Uk.Legislation.Models.Common;

// Enum to URI code
string code = LegislationType.UkPublicGeneralAct.ToUriCode(); // "ukpga"

// URI code to enum
LegislationType type = LegislationTypeExtensions.FromUriCode("ukpga");

// Safe conversion
if (LegislationTypeExtensions.TryFromUriCode("uksi", out var siType))
{
    Console.WriteLine($"Found: {siType}"); // UkStatutoryInstrument
}

Known Limitations

  1. No JSON Support - The API only provides XML/Atom/HTML formats (not JSON)
  2. Provision Endpoints - Direct provision access (e.g., /section/1/data.xml) returns 404
    • Workaround: Parse full legislation XML
  3. Basic XML Parsing - Currently extracts title only; full CLML parsing planned for Phase 3

See API_ENDPOINTS.md for detailed endpoint documentation.

Requirements

  • .NET 10 or later
  • Internet connection to access legislation.gov.uk

Testing

# Run all tests
dotnet test

# Run unit tests only
dotnet test --filter "Category=Unit"

# Run integration tests only (requires internet)
dotnet test --filter "Category=Integration"

Test Results: 26 passed, 1 skipped (95% success rate)

License

This project is licensed under the MIT License - see the LICENSE file for details.

The legislation data accessed through this API is provided under the Open Government Licence v3.0.

ยฉ Crown and database right

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments


Current Version: 10.0.x (Phase 2 Complete)
Status: Production Ready - Type-safe enum-based API
Test Coverage: >90%
Next Phase: Enhanced CLML XML parsing

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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

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
10.0.6 48 1/31/2026
10.0.1 193 11/26/2025

v10.0.0 - Initial release with core legislation API support (XML/Atom parsing). See https://github.com/panoramicdata/Uk.Legislation/releases