We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to use field on root of data object from within foreach?

This is hopefully an easy question that just needs to be made obvious in the documentation…

Consider the following example data object:

public class ExampleRoot
    public DateTime MainDate { get; set; }
    public List<ExampleChild> ExampleChildren { get; set; }
public class ExampleChild
    public string Name { get; set; }
    public DateTime ChildSpecificDate { get; set; }

And the following example template:

Report Document For <<[MainDate]:"MMMM d, yyyy">>

<<foreach [in ExampleChildren]>>
<<[Name]>> <<if [ChildSpecificDate != MainDate]>> (<<[ChildSpecificDate]>>) <</if>>

This obviously doesn’t work, because the scope inside the foreach is an ExampleChild object which doesn’t have a MainDate.

I do know that if I have nested foreaches, I can access the outer foreach by naming its iteration variable:

<<foreach [folder in folders]>>
Folder <<[FolderName]>>

<<foreach [in files]>>
<<[FileName]>> in <<[folder.FolderName]>>


Is there a mechanism like that for accessing the root of the data like I need to do above?

@grant.bowering You can pass name of the root object into the ReportingEngine.Build method as the last parameter, like shown in the example below:

Document doc = new Document(@"C:\Temp\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, root, "root");

In this case your template should look like the following:

 Report Document For <<[root.MainDate]:"MMMM d, yyyy">>
 <<foreach [in ExampleChildren]>>
 <<[Name]>> <<if [ChildSpecificDate != root.MainDate]>> (<<[ChildSpecificDate]>>) <</if>>

Please let me know if this is what you are looking for.

Thanks, that’s exactly what I needed!