保存无障碍验证结果 – 保存为字符串、JSON 和 XML

Aspose.HTML for .NET 提供了 Aspose.Html.Accessibility 命名空间,该命名空间用于所有与网页无障碍相关的操作和检查。在本文中,我们将探讨使用 Aspose.HTML .NET 库保存验证结果的过程,特别关注 ValidationResultSaveFormat 参数。

您可以从 无障碍验证器 – C# 中的网站无障碍检查 文章中了解如何使用 AccessibilityValidator 类检查网页无障碍性。

保存验证结果

网页无障碍验证对于确保网页内容符合 WCAG 规则和标准至关重要。验证过程完成后,您需要保存结果以进行进一步分析、文档记录和报告。我们的库允许您将验证结果保存到 System.IO.TextWriter 对象中,其中 ValidationResultSaveFormat 类型参数指定文本的保存格式。

ValidationResultSaveFormat

三种主要格式可用于保存网页无障碍验证结果:

将验证结果保存为字符串

将验证结果保存为字符串时,使用 SaveToString() 方法:

 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}

输出以简单的文本格式呈现,清楚地指示检查结果并提供有关错误的详细信息和注释:

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

其中显示了检查结果 validationResult 以及错误和注释列表:

以 XML 格式保存验证结果

对于那些喜欢更结构化和机器可读格式的人来说,以 XML 格式存储验证结果是一个合适的选择。让我们看看如何使用 SaveTo() 方法以 XML 格式保存结果。此方法接受一个 System.IO.TextWriter 对象和所需的 ValidationResultSaveFormat(在本例中为 XML)。

 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}

生成的 XML 表示是一种组织良好的格式,便于分析和进一步处理:

 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>

保存验证结果是网页无障碍检查中不可或缺的一步,有助于后续的分析、文档记录和报告。ValidationResultSaveFormat 参数提供了灵活性,允许您根据特定需求在文本、JSON 和 XML 格式之间进行选择。

另请参阅

  • 文章 屏幕阅读器无障碍性 解释了如何使用 Aspose.HTML for .NET API 根据 WCAG 指南设计屏幕阅读器无障碍的网站。
  • 如果您想了解如何根据 WCAG 标准(网页无障碍的世界权威)正确调整网页内容的对比度,请查看文章 颜色对比度无障碍性。您将了解如何使用 C# 测试颜色对比度无障碍性,并使所有用户都能轻松阅读您的网页内容。
  • 在文章 无障碍验证器 中,您将了解可用于测试网页无障碍规则(如原则、指南和标准)的 AccessibilityValidator 类。
  • 在文章 网页无障碍规则 中,您将学习如何使用 AccessibilityRules 类,该类是 WCAG 2 要求、成功标准和技术的存储库,用于网页无障碍检查。
  • 文章 错误和警告 讨论了在网站无障碍测试期间帮助收集错误和警告信息的类和接口。它侧重于失败标准和报告错误的方法,并提供了在测试 HTML 文档后检索网页无障碍错误的 C# 示例。

Aspose.HTML 提供免费的在线 网页无障碍检查器。此工具扫描网页,验证它们是否符合 WCAG 标准,识别问题并提出改进建议。立即了解您网站的合规性,让您能够确定必要更正的范围以及您的网站或 HTML 文档当前状态与 WCAG 要求之间的差距。

Text “Web Accessibility Checker”

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.