Save Accessibility Validation Results in C#

Save accessibility validation results in C# with ValidationResult.SaveToString() or ValidationResult.SaveTo(). Use ValidationResultSaveFormat to write text, JSON, or XML output for reporting, logging, and automated checks.

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:

  1. Create and run an accessibility validator for the HTML document.
  2. Receive a ValidationResult object from Validate(document).
  3. Call SaveToString() to serialize the result in text format.
  4. Send the returned string to the console, logs, or another reporting workflow.
 1// Validate HTML for accessibility and export all errors and warnings as a string
 2
 3string htmlPath = Path.Combine(DataDir, "input.html");
 4
 5using (HTMLDocument document = new HTMLDocument(htmlPath))
 6{
 7    AccessibilityValidator validator = new WebAccessibility().CreateValidator();
 8
 9    ValidationResult validationresult = validator.Validate(document);
10
11    // get rules errors in string format 
12    string content = validationresult.SaveToString();
13
14    // SaveToString - return only errors and warnings
15    // if everything is ok, it will return "validationResult:true"
16    Console.WriteLine(content);
17}

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. Create and run the accessibility validation workflow.
  2. Create a TextWriter for the target output.
  3. Pass the writer and ValidationResultSaveFormat.XML to SaveTo().
  4. Use the saved XML in reports, CI checks, or downstream processing.
 1// Validate HTML for accessibility and export all errors and warnings as an XML
 2
 3string htmlPath = Path.Combine(DataDir, "input.html");
 4
 5using (HTMLDocument document = new HTMLDocument(htmlPath))
 6{
 7    AccessibilityValidator validator = new WebAccessibility().CreateValidator();
 8    ValidationResult validationresult = validator.Validate(document);
 9    
10    using (StringWriter sw = new StringWriter())
11    {
12        validationresult.SaveTo(sw, ValidationResultSaveFormat.XML);
13        string xml = sw.ToString();
14
15        Console.WriteLine(xml);
16
17        try
18        {
19            XmlDocument doc = new XmlDocument();
20            doc.LoadXml(xml);
21        }
22        catch (Exception)
23        {
24            Console.WriteLine("Wrong xml format");
25        }
26    }
27}

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.

FAQ

Which formats can I use to save validation results?

You can save validation results as text, JSON, or XML by using ValidationResultSaveFormat with SaveTo(). SaveToString() returns text output.

When should I choose XML or JSON?

Use XML or JSON when validation results must be consumed by another tool, CI workflow, dashboard, or automated reporting pipeline.

Can I save validation results to a custom destination?

Yes. Use SaveTo() with a TextWriter connected to the destination required by your application, such as a file, memory buffer, or logging pipeline.

Related Articles

You can also use the online Web Accessibility Checker when you need a quick page scan before saving results in a C# workflow.