Skip to content

Commit 39a3587

Browse files
committed
Validate generated Docx files with C#'s OpenXmlValidator
1 parent 189579e commit 39a3587

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

Diff for: TestWordFiles/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
obj

Diff for: TestWordFiles/Program.cs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using DocumentFormat.OpenXml;
3+
using DocumentFormat.OpenXml.Packaging;
4+
using DocumentFormat.OpenXml.Validation;
5+
using Newtonsoft.Json;
6+
7+
class Program {
8+
static void ValidateWordDocument(string filePath) {
9+
try {
10+
var wordDoc = WordprocessingDocument.Open(filePath, true);
11+
var validator = new OpenXmlValidator();
12+
var count = 0;
13+
foreach (var error in validator.Validate(wordDoc)) {
14+
if (error.Description.StartsWith("The element has unexpectedx child")) {
15+
continue;
16+
}
17+
count++;
18+
Console.WriteLine("Error " + count);
19+
Console.WriteLine("Description: " + error.Description);
20+
Console.WriteLine("ErrorType: " + error.ErrorType);
21+
Console.WriteLine("Node: " + error.Node);
22+
Console.WriteLine("Path: " + error.Path.XPath);
23+
Console.WriteLine("Part: " + error.Part.Uri);
24+
Console.WriteLine("-------------------------------------------");
25+
}
26+
27+
Console.WriteLine("count={0}", count);
28+
}
29+
catch (Exception ex) {
30+
Console.WriteLine(ex.Message);
31+
}
32+
}
33+
34+
static void Main() {
35+
string[] files = System.IO.Directory.GetFiles("../tests", "_tmp_*.docx");
36+
foreach (string file in files) {
37+
Console.WriteLine("🆕 ===========================================");
38+
Console.WriteLine("Validating file: " + file);
39+
ValidateWordDocument(file);
40+
}
41+
}
42+
}

Diff for: TestWordFiles/TestWordFiles.csproj

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.1" />
12+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
13+
</ItemGroup>
14+
15+
</Project>

Diff for: TestWordFiles/TestWordFiles.sln

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWordFiles", "TestWordFiles.csproj", "{D73FB080-814B-606D-27F6-12CDD99E6878}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{D73FB080-814B-606D-27F6-12CDD99E6878}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{D73FB080-814B-606D-27F6-12CDD99E6878}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{D73FB080-814B-606D-27F6-12CDD99E6878}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{D73FB080-814B-606D-27F6-12CDD99E6878}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {32E5F859-BF84-4B82-BFA3-F80F3FCDF4AF}
23+
EndGlobalSection
24+
EndGlobal

Diff for: justfile

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ test-unit: node_modules
3535
node --test --disable-warning=ExperimentalWarning
3636

3737

38+
# Run C#'s OpenXmlValidator on the test Docx files
39+
test-docx-files:
40+
cd TestWordFiles && dotnet run
41+
42+
3843
# Run all the tests
3944
test: fmt lint test-unit
4045

0 commit comments

Comments
 (0)