Compilation errors not helpful
Whenever a template document is parsed using the DocumentFactory.Create() method and the parser find an issue with one of the expressions it the following exception is thrown:
Unhandled exception. SharpDocx.SharpDocxCompilationException: Exception of type 'SharpDocx.SharpDocxCompilationException' was thrown.
at SharpDocx.DocumentCompiler.Compile(String sourceCode, List`1 referencedAssemblies)
at SharpDocx.DocumentCompiler.Compile(String viewPath, String className, String baseClassName, Type modelType, List`1 usingDirectives, List`1 referencedAssemblies)
at SharpDocx.DocumentAssembly..ctor(String viewPath, Type baseClass, Type modelType)
at SharpDocx.DocumentFactory.Create(String viewPath, Object model, Type baseClassType, Boolean forceCompile)
at SharpDocx.DocumentFactory.Create[TBaseClass](String viewPath, Object model, Boolean forceCompile)
at SharpDocxTest.Program.Main(String[] args) in C:\Users\foo\Code\SharpDocxTest\Program.cs:line 104
This error message is completely generic. Debugging would be a lot easier, if the exception would contain some hint, which statement caused the compilation error
Catch the SharpDocx.SharpDocxCompilationException and look at the Errors and SourceCode properties.
example: try { ... your failing code trying to generate the document } catch (SharpDocx.SharpDocxCompilationException sdce) { StringBuilder sb = new StringBuilder(sdce.Errors); sb.AppendLine(sdce.SourceCode); Console.WriteLine(sb.ToString()); }
I also have the same problem, help solution Net7.0 development environment using System; using DocumentFormat.OpenXml.Wordprocessing; using SharpDocx.CodeBlocks; using SharpDocx.Models; using System.Collections.Generic;
namespace SharpDocx { public class SharpDocument_a9f547f718d341e5adcbab329d91254c : DocumentBase { public List`1 Model { get; set; }
protected override void InvokeDocumentCode()
{
CurrentTextBlockStack.Push(CurrentTextBlock);
CurrentTextBlock = CodeBlocks[0] as TextBlock;
CurrentCodeBlock = CodeBlocks[0];
foreach (var data in Model) {
CurrentCodeBlock = CodeBlocks[1];
Write(data .CompanyName );
CurrentCodeBlock = CodeBlocks[2];
Write(data.CandidateName );
CurrentCodeBlock = CodeBlocks[3];
Write(data.JobName );
CurrentCodeBlock = CodeBlocks[4];
Write(data.ActualEntryDateStr);
CurrentCodeBlock = CodeBlocks[5];
Write(data.ActualCharge);
CurrentCodeBlock = CodeBlocks[6];
Write(data.InvoiceActualTaxAmount);
CurrentCodeBlock = CodeBlocks[7];
Write(data.CandidateName);
AppendTextBlock();
CurrentCodeBlock = CodeBlocks[8];
}
CurrentTextBlock = CurrentTextBlockStack.Pop();
}
public override void SetModel(object model)
{
if (model == null)
{
Model = null;
return;
}
Model = model as List`1;
if (Model == null)
{
throw new ArgumentException("Model is not of type List`1", "model");
}
}
}
}