Linq reporting engine: How to use fields that might not exist

Hello,
I’m a newbie to the linq reporting engine.
I use xml files as data source. It might happen that some fields that are used in the doc template do not exist in the xml. It depends if that data exists in our backend data or not.

This is the xml-file:

<?xml version="1.0" standalone="yes"?>
<Data>
	<Anzahl>2</Anzahl>
	<NV>
        <Anrede>Hello</Anrede>
        <Anschrift1>Herrn</Anschrift1>
        <Anschrift2>Name of customer</Anschrift2>
        <Anschrift5>Street</Anschrift5>
        <Anschrift6>Town</Anschrift6>
    </NV>
</Data>

That’s how I try to handle the conditionally available field NV.Anschrift3 in the doc template:

<<[NV.Anschrift1]>>
<<[NV.Anschrift2]>>
<<if[NV.Anschrift3!=null]>><<[NV.Anschrift3]>><</if>>
<<if[NV.Anschrift4!=null]>><<[NV.Anschrift4]>><</if>>

That’s my test code:

[Test]
public void TestConditional()
{
    XmlDataSource source = new XmlDataSource(MyDir + "Mail merge data - ConditionalData.xml");

    Document doc = new Document(MyDir + "Reporting engine template - TestConditionalData.docx");

    ReportingEngine engine = new ReportingEngine();
    engine.BuildReport(doc, source);

    doc.Save(ArtifactsDir + "ReportingEngine.TestConditionalData.docx");
}

When calling engine.BuildReport it results in an exception:
System.InvalidOperationException: 'An error has been encountered at the end of expression ‘NV.Anschrift3!=n’. Can not get the value of member ‘Anschrift3’ on type ' ‘.’
As image:

I do not find any example how to handle fields that might be missing.
I tried with all forms of ? operator, but always run in a similar exception.

Would be great if anybody can give me hint how to solve that problem.

@rainer.zehnle

This is an expected behavior. Please note that missing object members are not the same as null values, so comparison with null does not work.

By default, the engine throws an exception in case of referencing a missing object member to indicate typos and similar errors. However, you can make the engine treat missing object members as null literals by applying ReportBuildOptions.AllowMissingMembers as follows:

ReportingEngine engine = new ReportingEngine();
engine.Options |= ReportBuildOptions.AllowMissingMembers;
engine.BuildReport(...);

Please check Accessing Missing Members of Data Objects for more information.

Thanks a lot @ivan.lyagin!
That was exactly what I was looking for and it immediately works as expected.

1 Like