We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert template from MUSTACHE syntax to LINQ syntax

Hello guys! Is it possible to convert word templates from MUSTACHE syntax to LINQ syntax?


Thanks a lot!
Hi Аhtoh,

Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you.

We suggest you please read about template syntax of Linq reporting engine from here:

Template Syntax

FindReplaceOptions options = new FindReplaceOptions();

Document doc = new Document(MyDir + "in.docx");

doc.MailMerge.UseNonMergeFields = true;

foreach (String item in doc.MailMerge.GetFieldNames())

{

Console.WriteLine(item);

doc.Range.Replace("{{ " + item + " }}", "<<["+ item + "]>>", options);

}

doc.Save(MyDir + "Out v16.11.0.docx");

Thanks Tahir! But will it convert all cases? Will it work for all docs with MUSTACHE syntax?

Hi Аhtoh,

Thanks for your inquiry. The mustache and LINQ template syntax is simple text and it is stored in Run nodes. Please read about Aspose.Words document object model from here:

Aspose.Words Document Object Model

You can find the text in the document and replace it with new text according to your requirement using Aspose.Words. Please refer to the following article:
Find and Replace Overview

In your case, we suggest you please update your template document manually using MS Word and use LINQ Reporting Engine.

Hello Tahir! Thanks for your reply!


Why doc.MailMerge.GetFieldNames() can return empty list?

Thanks a lot!
Hi Аhtoh,

Thanks for your inquiry. If you are using mustache syntax, please set the value of MailMerge.UseNonMergeFields to true as shown below. MailMerge.GetFieldNames then returns mail merge field names available in the document.

Document doc = new Document(MyDir + "in.docx");

doc.MailMerge.UseNonMergeFields = true;

Thanks Tahir!

Tahir,


I have a question regarding Mail Merging with LINQ template. Please find attached example document. I receive JSON data to merge with template, then I convert it to XML and apply BuildReport method. During merging I see error message that "An error has been encountered at the end of expression ‘Client.FirstName]>’. Can not get the value of member ‘FirstName’ on type ‘Newtonsoft.Json.Linq.JObject’."

This is the code I use:

var resultName = Guid.NewGuid() + “.docx”;
var tempPath = this._config.TempStoragePath + resultName;

var doc = new Document(documentTempPath);
var converter = new ExpandoObjectConverter();
dynamic message = JsonConvert.DeserializeObject(json, converter);
var engine = new ReportingEngine();
engine.BuildReport(doc, message, “Client”);
doc.Save(tempPath);

This is json value I use - “{Client: {FirstName: “Person”, LastName: “Zlatnika”, DOB: “Zlatnika2” } }”

Hi Аhtoh,

Thanks for your inquiry. Please use the following code example to get the desired output. Hope this helps you.

var json = "{Client: {FirstName: \"Person\", LastName: \"Zlatnika\", DOB: \"Zlatnika2\" } }";

XmlDocument Xml = (XmlDocument)JsonConvert.DeserializeXmlNode(json);

DataSet ds = new DataSet();

ds.ReadXml(new MemoryStream(Encoding.UTF8.GetBytes(Xml.InnerXml)));

var doc = new Document(MyDir + "LINQtemplate11.docx");

var engine = new ReportingEngine();

engine.BuildReport(doc, ds.Tables[0].Rows[0], "Client");

doc.Save(MyDir + "Out v16.11.0.docx");

Thanks a lot Tahir - that works like a charm!


One more question - can I use complex objects in my templates for example
“Person.Related.Car.Model”?

I can’t get it worked, aspose just replaces it with empty string.

This is an example of json I use: “{Person: { Related: { Car: { Model = “X2” } } }}”

I think I found a problem… I use DataSet objects and build it from XML, so we can’t put complex object into ItemArray element…


My question is - how I can solve it in my templates?
Hi Аhtoh,

Thanks for your inquiry. We suggest you please read following articles. Hope this helps you.

Template Syntax
Typical Templates

"Thanks Tahir for your reply! However that doesn’t help.


Can I use dynamic objects as datasource (because I have unique structure for each report - so I don’t know what object I need). When I use anonymous C# object I see this error

“A data source object must be of a visible type or implement”
Hi Аhtoh,

Thanks for your inquiry. Could you please share complete detail of your use case along with template documents? We will then provide you more information about your query.