Generate Document from XML Data Source
Leave feedback
GroupDocs.Assembly for .NET provides the XmlDataSource class to work with XML data directly in templates. Unlike using DataSet, XmlDataSource automatically recognizes data types (numbers, dates, booleans) from XML, making it easier to perform calculations and formatting.
The main classes involved are:
- XmlDataSource - loads and parses XML data
- DocumentAssembler - assembles documents
- XmlDataLoadOptions - configures XML loading behavior
Here are the steps to generate documents from XML:
- Create or obtain XML data (from file, string, or stream)
- Instantiate XmlDataSource with the XML data
- Create a template document with expression tags referencing XML elements
- Use DocumentAssembler to assemble the template with XmlDataSource
- Save the assembled document
NoteXmlDataSource automatically recognizes Int64, Double, Boolean, and DateTime values from XML strings, enabling numeric operations and date formatting in templates without explicit type conversion.
The simplest way to use XML data is from a file:
using GroupDocs.Assembly;
using GroupDocs.Assembly.Data;
public static void GenerateFromXmlFile()
{
XmlDataSource xmlDataSource = new XmlDataSource("data.xml");
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("Template.docx", "Output.docx",
new DataSourceInfo(xmlDataSource, "data"));
}
Your XML file (data.xml) might look like this:
<Person>
<Name>John Doe</Name>
<Age>30</Age>
<Birth>1989-04-01 4:00:00 pm</Birth>
<Email>john.doe@example.com</Email>
</Person>
For the XML above, your template can reference elements directly:
Name: <<[data.Name]>>
Age: <<[data.Age]>>
Date of Birth: <<[data.Birth]:"dd.MM.yyyy">>
Email: <<[data.Email]>>
You can also create XmlDataSource from an XML string:
using GroupDocs.Assembly;
using GroupDocs.Assembly.Data;
public static void GenerateFromXmlString()
{
string xmlString = @"<Person>
<Name>Jane Smith</Name>
<Age>28</Age>
<Birth>1992-01-31 07:00:00 am</Birth>
<Email>jane.smith@example.com</Email>
</Person>";
XmlDataSource xmlDataSource = new XmlDataSource(xmlString);
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("Template.docx", "Output.docx",
new DataSourceInfo(xmlDataSource, "person"));
}
When your XML root contains an array of elements, XmlDataSource treats it as a collection:
using GroupDocs.Assembly;
using GroupDocs.Assembly.Data;
public static void GenerateFromXmlArray()
{
string xmlArray = @"<Persons>
<Person>
<Name>John Doe</Name>
<Age>30</Age>
</Person>
<Person>
<Name>Jane Smith</Name>
<Age>28</Age>
</Person>
<Person>
<Name>Bob Johnson</Name>
<Age>35</Age>
</Person>
</Persons>";
XmlDataSource xmlDataSource = new XmlDataSource(xmlArray);
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("Template.docx", "Output.docx",
new DataSourceInfo(xmlDataSource, "persons"));
}
For XML arrays, use a foreach data band:
<<foreach [in persons]>>
Name: <<[Name]>>, Age: <<[Age]>>
<</foreach>>
Average age: <<[persons.Average(p => p.Age)]>>
XmlDataSource handles nested XML structures automatically:
using GroupDocs.Assembly;
using GroupDocs.Assembly.Data;
public static void GenerateFromNestedXml()
{
string nestedXml = @"<Company>
<Name>ABC Corp</Name>
<Address>
<Street>123 Main St</Street>
<City>New York</City>
<Zip>10001</Zip>
</Address>
<Employees>
<Employee>
<Name>John Doe</Name>
<Position>Developer</Position>
</Employee>
<Employee>
<Name>Jane Smith</Name>
<Position>Manager</Position>
</Employee>
</Employees>
</Company>";
XmlDataSource xmlDataSource = new XmlDataSource(nestedXml);
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("Template.docx", "Output.docx",
new DataSourceInfo(xmlDataSource, "company"));
}
Template for nested XML:
Company: <<[company.Name]>>
Address: <<[company.Address.Street]>>, <<[company.Address.City]>> <<[company.Address.Zip]>>
Employees:
<<foreach [in company.Employees]>>
- <<[Name]>>, <<[Position]>>
<</foreach>>
You can configure how XmlDataSource parses XML:
using GroupDocs.Assembly;
using GroupDocs.Assembly.Data;
public static void GenerateWithXmlOptions()
{
XmlDataLoadOptions options = new XmlDataLoadOptions();
options.AlwaysGenerateRootObject = true; // Always create root object
XmlDataSource xmlDataSource = new XmlDataSource("data.xml", options);
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("Template.docx", "Output.docx",
new DataSourceInfo(xmlDataSource, "data"));
}
WarningBy default, if a root XML element contains only a sequence of elements of one type, the engine skips the root object. SetAlwaysGenerateRootObjectto true to always include it.
To learn more about working with XML schemas, complex XML structures, XML attributes, and advanced XmlDataSource options, please refer to the advanced usage section.
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Assembly for .NET examples, plugins, and showcase
- GroupDocs.Assembly for Java examples, plugins, and showcase
- GroupDocs.Assembly for Python via .NET examples, plugins, and showcase
Along with the full-featured .NET library, we provide simple but powerful free online apps.
To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.