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:
- Text
- JSON
- XML
Saving Validation Results to String
When saving validation results to a string, the SaveToString()
method is used:
1var htmlPath = Path.Combine(DataDir, "input.html");
2
3using (var document = new HTMLDocument(htmlPath))
4{
5 var validator = new WebAccessibility().CreateValidator();
6
7 var validationresult = validator.Validate(document);
8
9 // get rules errors in string format
10 var 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}
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:
- technique – code of the technique that was tested
- criterion – criteria to which the technique corresponds
- type – result type
- description – error description
- source – tag containing the error
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).
1var htmlPath = Path.Combine(DataDir, "input.html");
2
3using (var document = new HTMLDocument(htmlPath))
4{
5 var validator = new WebAccessibility().CreateValidator();
6 var validationresult = validator.Validate(document);
7
8 using (var sw = new StringWriter())
9 {
10 validationresult.SaveTo(sw, ValidationResultSaveFormat.XML);
11 var 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}
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.