Save Validation Results – Web Accessibility Check in C#

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 htmlFile = "input.html";
2using (var document = new Aspose.Html.HTMLDocument(filePath))
3{
4    var result = RulesValidate(document, ValidationBuilder.All);
5    var str = result.SaveToString();
6    Console.WriteLine(str);
7}

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).

 1 string htmlFile = "input.html";
 2 using (var document = new Aspose.Html.HTMLDocument(htmlFile))
 3 {
 4     var result = RulesValidate(document, ValidationBuilder.All);
 5     using (var sw = new StringWriter())
 6     {
 7         result.SaveTo(sw, ValidationResultSaveFormat.XML);
 8         var xml = sw.ToString();
 9
10         Console.WriteLine(xml);
11
12         try
13         {
14             XmlDocument doc = new XmlDocument();
15             doc.LoadXml(xml);
16         }
17         catch (Exception)
18         {
19             Console.WriteLine("Wrong xml format");
20         }
21     }
22 }

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.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.