XML is a W3C Recommended, self-descriptive language that is designed to store and transport data. A developer can change the XML format into any other better human-readable format like PDF or MS Word document within the .NET application. This article will discuss how to convert XML data into PDF and MS Word reports using C# via simple templates.
The following topics are discussed below:
- .NET API for Generating Reports
- Generate PDF Report from XML Data using C#
- Generate MS Word DOC/DOCX Report from XML Data using C#
Report Generation .NET API – XML to PDF and WORD
GroupDocs.Assembly for .NET is the API to automate the report generation from the XML data using DOCX or TXT template. Additionally, it supports the JSON, CSV, and other data sources to convert the data into reports of different file formats.
PM> Install-Package GroupDocs.Assembly
Generate PDF Report from XML Data in C#
3 simple steps will lead you to convert XML data into the formatted PDF report.
- Load your XML data source.
- Define the template according to the loaded XML data.
- Finally, provide XML data source and template to a report generation method.
The following XML sample data is used to get it converted to the PDF report. It contains the data of managers and their respective clients with some more details.
<Managers> <Manager> <Name>John Smith</Name> <Contract> <Client> <Name>A Company</Name> </Client> <Price>1200000</Price> </Contract> <Contract> ... </Contract> ... </Manager> <Manager> <Name>Tony Anderson</Name> ... </Manager> ... </Managers>
Define the template in TXT or DOCX format according to your source XML data. I am using the below mentioned template that is created according to the above mentioned XML data of managers. This will make the report generator to iterate over Managers and their respective Clients. After the completion of template, you are almost done. You can use the code below for your report generation.
<<foreach [in managers]>>Manager: <<[Name]>> Contracts: <<foreach [in Contract]>>- <<[Client.Name]>> ($<<[Price]>>) <</foreach>> <</foreach>>
C# Steps to Generate PDF Report from XML
The following steps allow you to automate the generation of PDF reports from your XML data according to your defined template.
- Define XML data file, text template file, and PDF output report files.
- Instantiate XMLDataSoure with XML data file.
- Create DataSourceInfo with defined XML data source.
- Call the AssembleDocument method to generate the PDF report.
The following code implements the above steps and generates a PDF from the XML data source using C#.
Generate MS Word Report from XML data in C#
In the same vein, you can also create the report in MS Word DOC/DOCX format using the same XML data. There will be no difference in the code from the one we discussed above, except that you have to change the output file name.
- Load the XML data file.
- Defining the template in TXT or DOCX format.
- Set the output report document format as DOCX.
- Provide the XML data file, template, and output file path to DocumentAssembler to convert the XML to DOCX.
The following code converts the XML and generates the DOCX file using the defined template using C#.
Get a Free API License
You can get a free temporary license in order to use the API without the evaluation limitations.
To sum up, you have learned to convert the XML data into PDF format as a report using C# with the .NET application. Additionally, we discussed the report generation in DOC/DOCX format from the same XML using the template. After reading this series of report generation posts; Generate PDF and MS Word Reports from JSON, CSV, XML, you can develop your own report building .NET application.