Create word document with input from XML

Hello,

We have created our own templates/models (docx) that we open to create a new document. The input to fil the document is retrieved from an XML. The template consits of includetext and If statement.
I know we can do this with Aspose but can someone tell me how the flow of the processing is from the start when we open the template/model. We do this with OpenXML and the software we use is C##.

@mars63,

Thanks for your interest in Aspose.Words for .NET API. You may please consult documentation to achieve what you are looking for. In case you face any problems, please ZIP and upload your simplified input Word document (containing includetext and If statement), sample XML file, Aspose.Words generated output DOCX file showing the undesired behavior and your expected file showing the correct output here for testing. You can create expected Word document by using MS Word. We will then investigate the issue on our end and provide you more information.

Isn’t it great that the only reply when asking for help switching from a competing product is essentially “RTFM”? The linked documentation provides essentially no information on working with XML in this way…

@CedH Could you please elaborate your requirements in more details?
If you XML is MS Word 2003 0r 2007 XML, then you can simply load it into the Document and save in any supported format, for example:

Document doc = new Document(@"C:\Temp\in.xml");
doc.Save(@"C:\Temp\out.pdf");

If your XML contains data you need to use as a data source for filling your template with data - you can use Mail Merge or LINQ Reporting Engine to achieve this.
So, please provide your sample XML and describe your scenario in more details, so we can help you.

Requirement: users need to be able to create templates where they can use a predefined list of variables. The application creates documents based on those templates and will fill in those variables.

We had solved this with mail merge, but it has a few drawbacks: the mail merge field name is constrained to 40 characters, and there can only be (IIRC) 63 fields in total. The solution is no longer sufficient, so I was looking for alternatives.
Selecting the variables via XML could be a possibility, which is probably what the OP was doing using OpenXML, but the Aspose documentation is not very clear on how to do the same. I did find Binding Content Control to Custom XML Parts which seems to be what is required but haven’t gotten it to work yet.

@CedH Thank you for additional information. Mapping to the values in the XML is achieved using the appropriate xPath to get this value, which is specified using XmlMapping.SetMapping. In your case you can create a dummy XML part and add it to template, which will be edited by the customer. For example:

<books>
	<book>
		<title>Book Title</title>
		<author>Book Author</author>
	</book>
</books>

In MS Word go to Developer tab and click XML Mapping Pane


On the pane add mapping to the dummy XMl part, once done the template editor will be able easily insert Structured Document Tags with the required xPath and mapping set:

Just right click on the required field and select Insert Content Control
Once done with template, in the code you can replace dummy XML with real data XML, for exampple:

<books>
	<book>
		<title>Code Complete</title>
		<author>Steve McConnell</author>
	</book>
</books>

And run code like this:

// Open template
Document doc = new Document(@"C:\Temp\in.docx");

// Replace dummy XML part with real data
doc.CustomXmlParts[0].Data = File.ReadAllBytes(@"C:\Temp\data.xml");
            
// Save output.
doc.Save(@"C:\Temp\out.docx");

Here is my simple template and output document: in.docx (20.1 KB) out.docx (17.2 KB)

Also, if you need to replace Mail Merge, you can consider using LINQ Reporting Engine, which was designed to overcome Mail Merge restrictions. LINQ Reporting Engine allows using XML as a data source for filling document with data.

Thanks for the information. With LINQ Reporting Engine, the tags need to be written by the template creator manually? Is there a way for them to select the possible tags from a list (in MS Word)?

@CedH Yes, the tags need to be written by the template creator manually. Unfortunately, there is no way to select tags from the list in MS Word.