Free Support Forum -

How can I use XML as a data source for a document?

We would like to generate Word documents from existing general purpose complex (i.e. not flat) XML documents. How can I use XML as a data source for a document? How can merge fields be defined that map to the proper element data?
Here is an example of a source XML document. We would like to be able to markup the Word template with fields by the element names:

<?xml version="1.0" encoding="utf-8" ?>
Peer Assessment
SMITH,Scott Steven (67765)
John Smith
Scott Steven
69 Red Oaks Road

L8N 1B9
(444) 666-7444



Hi Todd,

At the moment the only way to use XML data in Aspose.Word is to load it into a DataSet and merge with the DataSet.

Your XML files seems a bit strange to me. I would say that the repeated blocks inside Activity
should be wrapped into elements such as ActivityItem, in this case loading into DataSet will create two data tables Activity and ActivityItem and merge can be performed similar to the Order demo (Order and OrderDetails).

In the next version you will be able to insert rich document content at merge fields or bookmarks in the document. This will basically allow to still have a template but avoid using mail merge, say for irregularly shaped data and still insert it into the document directly. If you cannot change your XML format you could try this approach.

Hi Roman - thanks for your reply.

Yes, the XML looks a little unusual. It’s actually been altered from the original schema (where each “Activity item” was named generically - i.e. “ActivityRole”) to instead use specific names for each item. This was done to allow each item to be used atomically as a merge field.
For instance, the SubjectPhysician last name, first name and address items all need to be available as individual merge fields in a form letter. Information related to another role, such as AssessmentCoordinator, also needs to be available for use.
We have a large number of document templates that we need to create, so we are trying to design a general purpose solution, rather than having to code for each new template. Do you have any suggestions?

I’m afraid I don’t fully understand the problem domain and cannot give specific recommendations, here are some general useful hints:

1. Can you load your XML into a DataSet? If yes, then you probably can merge it into a document no problem because Aspose.Word will accept a DataSet.

2. If you cannot load your XML into a DataSet straight away, you still can massage it and fill a DataSet or a DataTable manually or through some transformations. Then feed into Aspose.Word.

3. If you have some tables or portions inside a document that need to grow to accommodate multiple records then you might need to define and use one or more mail merge regions.

4. Check out DocumentBuilder class available in the latest Aspose.Word 1.4. With DocumentBuilder you can navigate to any merge field in the document and insert any content. You can even build whole document programmatically if you want to.

The best way is to try to implement a POC with one of the templates and see if you can get everything workins the way you want. We will provide help and guidance. If you discover you cannot accomplish certain things we might implement them in the next version.

Hi again Roman,
Thanks again for your responses. Now I’d like to step back and ask some more general questions about Aspose.Word - I’ll start a new thread.