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

Free Support Forum - aspose.com

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 com.aspose.words.net.System.Data.DataRow’.

The xml fragment would look like the following :


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

<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”><<foreach
[c in commonCoverage]>>
<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”><<foreach
[pp in prop]>>…<>
<o:p></o:p>
<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”><>
<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”>
<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”>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.
<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”>
<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”>1) Is there a way to check whether the top XML element has sub elements in LINQ and conditionally apply foreach?
<span lang=“EN-CA” style=“font-size:10.0pt;font-family:
“Calibri”,“sans-serif”;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
“Times New Roman”;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;
mso-ansi-language:EN-CA;mso-fareast-language:EN-CA;mso-bidi-language:AR-SA”>
I see a similar issue has been reported in http://www.aspose.com/community/forums/663970/linq-xml/showthread.aspx.

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 123Descr 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 … 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"?>
Test
123Descr

Regards,
Manu

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.
///
AllowMissingMembers = 1

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

Best regards,