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

Free Support Forum - aspose.com

Accessing chlid element in XML structure for use in word

Hello,


I’ve got a xml node which looks like this:


DeadWeight is an element which is part of a region.
I’m using MailMerge.ExecuteWithRegions().

Within my word document I want to easyly access e.g. DeadWeight.kN by using a MERGEFIELD.

Is there a kind of notation for direct access xml fields?

You can obtain further information on how I currently try use Aspose.Word from this topic:
https://forum.aspose.com/t/63243

Kind regards

Hi Patrick,


Thanks for your inquiry. I think, you can achieve what you need after reading the article suggested below:
http://docs.aspose.com/display/wordsnet/How+to++Mail+Merge+from+XML+using+LINQ+to+XML

I hope, this will help.

Best Regards,

Well I don’t get it.


According to your example I have to obtain the tablename if I want to use execute with region.
That means my code has to know each Table:Start.

Is there a way to find all regions (tables) within a document?
'Cause I don’t want my code to have to know every specific tablename instead it should
remain dynamic.

Kind regards

Hi
Patrick,


Thanks for your inquiry. Please try using the following code snippet to get a collection of Mergefield names used to identify the beginning and ending of the mail merge regions in your document.

ArrayList regionNames = new ArrayList();<o:p></o:p>

string[] fieldNames = doc.MailMerge.GetFieldNames();

foreach (string name in fieldNames)

{

if (name.StartsWith("TableStart:") || name.StartsWith("TableEnd:"))

{

string regionName = name.Split(new char[]{':'})[1];

if (!regionNames.Contains(regionName))

regionNames.Add(regionName);

}

}


I hope, this will help.

Best Regards,

Hi Patrick,


Thanks for your inquiry.

An element with attributes is read into a DataSet using the ReadXml method as a child data table, in your case called “DeadWeight” and the attributes’ names and values appear in that table as the column names and values respectivtly.

So in your case you should simply create a child region within your template called “DeadWeight” and add the kN merge field within that region. I have attached a sample template demonstrating this.

Also note in the next version of Aspose.Words we will release some new mail merge functionality which allows merging attributes just like this much easier. We will inform you as soon as this is ready and we can see if it is useful for you.

Thanks,

Hi Patrick,


The latest version of Aspose.Words adds new syntax to the mail merge engine which is exactly what you are looking for.

You can now use plain text markers as merge fields and use Object.Attribute syntax. Please see below.

{{#foreach AdditionalAttachment}}

SIValue: {{DeadWeight.SIValue}}

kN Value: {{DeadWeight.kN}}

ProjectionArea Value: {{ProjectionArea}}
{{/foreach AdditionalAttachment}}


In order to use this feature you have to enable MailMerge.UseNonMergeFields. Please see the code below.

Document doc = new Document("MailMergeTemplate.doc");

DataSet dataSource = new DataSet();

dataSource.ReadXml("TestData.xml");

doc.MailMerge.UseNonMergeFields = true;

doc.MailMerge.ExecuteWithRegions(dataSource);


I have attached a sample template and XML for you to look at.

For further information please see the following thread:
Thanks,

Also you can still use existing merge fields and still use the Object.Attribute functionality. The field would simply look like this: MERGEFIELD DeadWeight.kN.


Thanks,

Well thank you for that! It’s exactly what I’ve been looking for.


This is called support. :slight_smile:
Hi,

Thanks for your feedback. It's great you were able to find what you were looking for. Please let us know any time you have any further queries.We are always glad to help you.

Best Regards,