Validate HTML Accessibility with a .NET API
Aspose.HTML for .NET provides an AccessibilityValidator for checking HTML documents against WCAG accessibility rules. Validate a local file or webpage, identify failed checks and affected elements, and export the results as a report.
Regular validation helps development teams find accessibility problems before publication and apply the same checks whenever HTML content changes.
Validate an HTML Document in C#
Create a validator with all available rule settings, validate the document, and iterate only through unsuccessful results. Read ruleResult.Results so every failed technique is reported even when its separate Errors collection is empty.
C# code to validate HTML accessibility
using System;
using System.IO;
using Aspose.Html;
using Aspose.Html.Accessibility;
using Aspose.Html.Accessibility.Results;
string documentPath = Path.Combine("data", "input.html");
WebAccessibility webAccessibility = new WebAccessibility();
AccessibilityValidator validator = webAccessibility.CreateValidator(
ValidationBuilder.All);
// Validate the HTML document against the configured rules
using HTMLDocument document = new HTMLDocument(documentPath);
ValidationResult validationResult = validator.Validate(document);
foreach (RuleValidationResult ruleResult in validationResult.Details)
{
if (ruleResult.Success)
{
continue;
}
Console.WriteLine($"{ruleResult.Rule.Code}: {ruleResult.Rule.Description}");
// Inspect every technique result, including entries without an error object
foreach (ITechniqueResult techniqueResult in ruleResult.Results)
{
if (techniqueResult.Success)
{
continue;
}
Console.WriteLine(
$" {techniqueResult.Rule.Code}: {techniqueResult.Rule.Description}");
IError error = techniqueResult.Error;
if (error == null)
{
continue;
}
Console.WriteLine($" {error.ErrorTypeName}: {error.ErrorMessage}");
if (error.Target?.TargetType == TargetTypes.HTMLElement &&
error.Target.Item is HTMLElement element)
{
Console.WriteLine($" {element.OuterHTML}");
}
}
}
How to Validate HTML Accessibility
- Create a WebAccessibility object, which provides access to validator creation and the accessibility rules repository.
- Call
CreateValidator(ValidationBuilder.All)to create a validator for all rules that match the complete builder settings. - Load a file or URL into an HTMLDocument .
- Call
Validate()and store the returned ValidationResult . - Iterate through
Details, keep the unsuccessfulRuleValidationResultobjects, and inspect failed techniques inResults. Print error details and the affected HTML element when the result provides them.
Configure and Process Accessibility Checks
- Complete validation.
ValidationBuilder.Allapplies every available level and technology setting to the rules in the repository. - Filtered validation. A validator can be restricted by level, technology, HTML tag, principle, guideline, criterion, or a list of rules.
- Detailed diagnostics.
ValidationResult.Detailsprovides rule results, while technique errors expose messages and affected targets. - Local and remote HTML.
HTMLDocumentcan load an HTML file or a webpage URL before validation. - Exportable results. Reports can be processed in code or saved as text, JSON, or XML for reviews and automated workflows.
- Manual verification. Automated results must be combined with keyboard, assistive-technology, content, and usability testing.
For an occasional no-code scan, use the online Web Accessibility Checker . Use the .NET API when validation must be configured, repeated, or integrated into a development pipeline.
Accessibility Validator Documentation
The
Accessibility Validator
guide explains the available CreateValidator() overloads and ValidationBuilder filters. See
Errors and Warnings
to process failed techniques and target elements, or
Save Validation Results
to export reports as text, JSON, or XML.
Get Started with Aspose.HTML for .NET API
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
HTML Accessibility Validator FAQ
1. How do I validate HTML accessibility in C#?
Load the HTML file or URL with HTMLDocument, create an AccessibilityValidator from WebAccessibility, and call Validate(). The returned ValidationResult contains the status and detailed results for the rules evaluated by the validator.
2. Can an automated accessibility validator guarantee WCAG compliance?
No. Automated validation can identify machine-testable failures, but many requirements depend on context and human judgment. Manual keyboard testing, assistive-technology testing, and review of content meaning are still required.
3. Can the validator check both local HTML files and webpages?
Yes. HTMLDocument can load a local file or a URL. Remote validation depends on network access and on whether the document can retrieve the stylesheets, scripts, and other resources needed by the selected checks.
4. Can I validate only selected WCAG rules or levels?
Yes. Use ValidationBuilder to filter by level or technology, or create the validator with a principle, guideline, criterion, or explicit list of rules. This is useful when a pipeline needs a focused policy instead of the complete repository.
5. Can accessibility validation results be saved?
Yes. Use SaveToString() for a text representation or SaveTo() with ValidationResultSaveFormat to write text, JSON, or XML. Structured output can be archived or consumed by reporting and quality-control workflows.
Related Web Accessibility Workflows
Continue with selected WCAG rules, image alt checks, color contrast validation, or the accessibility API overview.