File tree 5 files changed +88
-0
lines changed
5 files changed +88
-0
lines changed Original file line number Diff line number Diff line change
1
+ bin
2
+ obj
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -35,6 +35,11 @@ test-unit: node_modules
35
35
node --test --disable-warning=ExperimentalWarning
36
36
37
37
38
+ # Run C#'s OpenXmlValidator on the test Docx files
39
+ test-docx-files :
40
+ cd TestWordFiles && dotnet run
41
+
42
+
38
43
# Run all the tests
39
44
test : fmt lint test-unit
40
45
You can’t perform that action at this time.
0 commit comments