LINQ + XML foreach breaks for missing elements in XML

Hi Team,
I am using Aspose Words for Java for generating reports based on Docx templates. We use LINQ for binding data in XML to the template. One issue I am facing right now is that while iterating over an XML element using foreach, and if the element is missing in the XML, the binding fails with an exception like,
Caused by: java.lang.IllegalStateException: An error has been encountered at the end of expression 'pp in prop]>'. Can not get the value of member 'prop' on type 'class'.
The xml fragment would look like the following :


In the Docx template, the above XML is rendered with the following LINQ statements :

<<foreach[c in commonCoverage]>>  
    <<foreach [pp in prop]>>....<</foreach>>

The problem is that sometimes the XML fragment may not have the child element ‘prop’, in that event, the highlighted LINQ expression breaks with an exception.

  1. Is there a way to check whether the top XML element has sub elements in LINQ and conditionally apply foreach?

I see a similar issue has been reported in

Thanks and Regards,Manu

Hi Manu,

Thanks for your inquiry. Have you tried latest version of Aspose.Words for Java 15.10.0? In case the problem still remains, please zip and attach 1) your input template Word document, XML file and simplified Java source code you’re using on your end to build report. We will replicate the same issue on our end and provide you more information.

Best regards,

Hi Team,
Thanks for the response…But unfortunately, with the latest version of Aspose Word Java, I could still reproduce the issue…
Attached is the zip containing the template, XML and Java source for generating the report. To reproduce the issue, remove <items><item><itemNo>123</itemNo><description>Descr</description></item></items> from the XML and run the application.
In summary, following is my XML, and the requirement is that, I need to have a way to generate the report in case the <items>...</items> is missing from the XML. Currently, the foreach iteration over ‘items’ tag will break the flow.
Is there a way in LINQ to make a check like, topElement.hasElements(‘items’)?

<?xml version="1.0" encoding="UTF-8" ?>


Hi Manu,

Thanks for your inquiry. We tested the scenario and have managed to reproduce this problem on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-12653. Our product team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-12653) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi Manu,

It is to update you that we have introduced ReportBuildOptions.AllowMissingMembers property. The description is s follows:

/// Specifies that missing object members should be treated as null literals by the engine. This option
/// affects only access to instance (that is, non-static) object members and extension methods. If this
/// option is not set, the engine throws an exception when encounters a missing object member.

You need to use this option to meet your requirement. Hope, this helps.

Best regards,