Working With Data Sources
This page shows how to connect templates to the data structures that drive your reports.
It covers direct object binding, file-based data sources, and practical patterns for collections and dictionaries inside a report model.
Wordize accepts both ordinary .NET objects and specialized data-source classes. Choose the source type that matches where your data already lives.
Plain .NET Objects
If your application already has a report model, you can pass it directly.
Use the shared sample model and sample data setup from Shared Sample Model and Data.
Usage:
ReportBuilder.BuildReport("Template.docx", "Output.docx", booking, "booking");Template:
Support language: <<[booking.Metadata["SupportLanguage"]]>>
Segment count: <<[booking.Segments.Count()]>>Expected output:
Support language: English
Segment count: 3JSON Data
Use JsonDataSource when the report input arrives as JSON rather than a prebuilt object graph:
JsonDataLoadOptions options = new JsonDataLoadOptions
{
AlwaysGenerateRootObject = true,
PreserveSpaces = true
};
JsonDataSource json = new JsonDataSource("booking.json", options);
ReportBuilder.BuildReport("BookingTemplate.docx", "BookingOutput.docx", json);Example JSON:
{
"Reference": "BK-48291",
"PrimaryTraveler": "Avery Chen",
"Tags": ["priority", "summer-sale"]
}Template:
Reference: <<[Reference]>>
Tags:
<<foreach [in Tags]>>- <<[Tags_Text]>>
<</foreach>>When a JSON property is an array of simple values (for example, strings or numbers), use the property name with the _Text suffix to access item values in a template.
Example: use Tags to address the array itself, and Tags_Text to address its simple item values.
Expected output:
Reference: BK-48291
Tags:
- priority
- summer-saleXML Data
Use XmlDataSource when the source data already exists as XML. You can load XML from a file or stream, and you can also provide an XSD when your application needs schema-aware parsing:
XmlDataLoadOptions options = new XmlDataLoadOptions
{
AlwaysGenerateRootObject = true
};
XmlDataSource xml = new XmlDataSource("booking.xml", options);
ReportBuilder.BuildReport("BookingTemplate.docx", "BookingOutput.docx", xml);Example XML:
<Booking>
<Reference>BK-48291</Reference>
<PrimaryTraveler>Avery Chen</PrimaryTraveler>
</Booking>Template:
Reference: <<[Reference]>>
Primary traveler: <<[PrimaryTraveler]>>Expected output:
Reference: BK-48291
Primary traveler: Avery ChenCSV Data
CSV works well for flat, row-oriented datasets such as passenger manifests, inventory snapshots, or reconciliation reports:
CsvDataLoadOptions options = new CsvDataLoadOptions
{
HasHeaders = true,
Delimiter = ',',
QuoteChar = '"'
};
CsvDataSource csv = new CsvDataSource("travelers.csv", options);
ReportBuilder.BuildReport("TravelerListTemplate.docx", "TravelerListOutput.docx", csv, "travelers");Example CSV:
FullName,Seat
Avery Chen,12A
Noah Patel,14CTemplate:
<<foreach [row in travelers]>>- <<[row.FullName]>> | <<[row.Seat]>>
<</foreach>>Expected output:
- Avery Chen | 12A
- Noah Patel | 14CMultiple Sources in One Report
Some reports combine separate sources, for example a booking object and an agency profile:
Booking booking = GetBooking();
AgencyProfile agency = GetAgencyProfile();
ReportBuilder.BuildReport(
"CombinedTemplate.docx",
"CombinedOutput.docx",
new object[] { booking, agency },
new string[] { "booking", "agency" });Template:
Booking: <<[booking.Reference]>>
Issued by: <<[agency.DisplayName]>>Collections and Dictionaries
Collections and dictionaries behave naturally inside templates:
- Iterate lists with
foreach - Query arrays and lists with LINQ-style extension methods
- Access dictionary values with an index expression
Example:
Meal code: <<[booking.Metadata["MealCode"]]>>
Segment count: <<[booking.Segments.Count()]>>