Save Accessibility Validation Results – Save to String, JSON, and XML

Aspose.HTML for .NET provides the Aspose.Html.Accessibility namespace, which is intended for all web accessibility related manipulations and checks. In this article, we will explore the process of saving validation results using the Aspose.HTML .NET library, specifically focusing on the ValidationResultSaveFormat parameter.

You can learn how to use the AccessibilityValidator class to check web accessibility from the article Accessibility Validator – Website Accessibility Check in C#.

Saving Validation Results

Web accessibility validation is critical to ensuring that web content adheres to WCAG rules and standards. Once the validation process is complete, you need to save the results for further analysis, documentation, and reporting. Our library allows you to save the validation results into a System.IO.TextWriter object, where a ValidationResultSaveFormat type parameter specifies in what format the text will be saved.

ValidationResultSaveFormat

Three main formats are available for saving web accessibility validation results:

Saving Validation Results to String

When saving validation results to a string, the SaveToString() method is used:

 1string htmlPath = Path.Combine(DataDir, "input.html");
 2
 3using (HTMLDocument document = new HTMLDocument(htmlPath))
 4{
 5    AccessibilityValidator validator = new WebAccessibility().CreateValidator();
 6
 7    ValidationResult validationresult = validator.Validate(document);
 8
 9    // get rules errors in string format 
10    string content = validationresult.SaveToString();
11
12    // SaveToString - return only errors and warnings
13    // if everything is ok, it will return "validationResult:true"
14    Console.WriteLine(content);
15}
Example-SaveToString hosted with ❤ by GitHub

The output is presented in a simple text format, clearly indicating the result of the check and providing detailed information about errors with comments:

 1validationResult:False;
 2%%
 3technique: H35;
 4criterion: 1.1.1;
 5type: Error;
 6description: Check that the applet element contains an alt attribute with a text alternative for the applet. ;
 7source: <applet code="tictactoe.class" width="250" height="250">tic-tac-toe game</applet>;
 8%%
 9technique: H37;
10criterion: 1.1.1;
11type: Error;
12description: Img element missing an alt attribute. The value of this attribute is referred to as "alt text".;
13source: <img src="image1.jpeg">;
14%%
15
16...

Where the result of the check is indicated validationResult and a list of errors and comments:

Saving Validation Results in XML Format

For those who prefer a more structured and machine-readable format, storing validation results in XML is a suitable choice. Let’s look at how to save the results in XML format using the SaveTo() method. This method takes a System.IO.TextWriter object and the desired ValidationResultSaveFormat (in this case, XML).

 1string htmlPath = Path.Combine(DataDir, "input.html");            
 2
 3using (HTMLDocument document = new HTMLDocument(htmlPath))
 4{
 5    AccessibilityValidator validator = new WebAccessibility().CreateValidator();
 6    ValidationResult validationresult = validator.Validate(document);
 7    
 8    using (StringWriter sw = new StringWriter())
 9    {
10        validationresult.SaveTo(sw, ValidationResultSaveFormat.XML);
11        string xml = sw.ToString();
12
13        Console.WriteLine(xml);
14
15        try
16        {
17            XmlDocument doc = new XmlDocument();
18            doc.LoadXml(xml);
19        }
20        catch (Exception)
21        {
22            Console.WriteLine("Wrong xml format");
23        }
24    }
25}
Example-OutputToXML hosted with ❤ by GitHub

The resulting XML representation is a well-organized format for easy analysis and further processing:

 1<validationResult>
 2<isValid>false</isValid>
 3<details>
 4  <techniqueResult>
 5    <technique>H35</technique>
 6    <criterion>1.1.1</criterion>
 7    <type>Error</type>
 8    <description>Check that the applet element contains an alt attribute with a text alternative for the applet. </description>
 9    <source><![CDATA[<applet code="tictactoe.class" width="250" height="250">tic-tac-toe game</applet>]]>
10    </source>
11  </techniqueResult>
12  <techniqueResult>
13    <technique>H37</technique>
14    <criterion>1.1.1</criterion>
15    <type>Error</type>
16    <description>Img element missing an alt attribute. The value of this attribute is referred to as "alt text".</description>
17    <source><![CDATA[<img src="image1.jpeg">]]>
18    </source>
19  </techniqueResult>
20  
21   ...
22
23 </details>
24</validationResult>

Saving validation results is an integral step in web accessibility checking and facilitates subsequent analysis, documentation, and reporting. The ValidationResultSaveFormat parameter provides flexibility by allowing you to choose between Text, JSON, and XML formats based on your specific needs.

See Also

  • The article Screen Reader Accessibility explains how to design a website for screen reader accessibility in accordance with WCAG guidelines using the Aspose.HTML for .NET API.
  • Check out the article Сolor Сontrast Accessibility if you want to learn how to properly adjust the contrast of your web content to WCAG standards, the world authority on web accessibility. You will discover how to test color contrast accessibility using C# and make your web content easy to read for all users.
  • In the article Accessibility Validator, you will learn about the AccessibilityValidator class that can be used to test web accessibility rules such as principles, guidelines, and criteria.
  • In the article Web Accessibility Rules, you will learn how to use the AccessibilityRules class, which is a repository of WCAG 2 requirements, success criteria, and techniques, for web accessibility checks.
  • The article Errors and Warning discusses the classes and interfaces that help collect information about errors and warnings during website accessibility testing. It focuses on the failure criteria and methods that report errors, and provides a C# example for retrieving web accessibility errors after testing an HTML document.

Aspose.HTML offers free online Web Accessibility Checker. This tool scans web pages, validates them for WCAG compliance, identifies problems, and suggests improvements. Get instant insights into your website’s compliance, allowing you to determine the scope of necessary corrections and the gap between the current state of your website or HTML document and WCAG requirements.

Text “Web Accessibility Checker”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.