How to remove a mail merge region if the value inside the region is null or empty string

I have a document as attached
RefferalJD.docx (28.1 KB)
i want to hide the complete row (2nd row of JobTitle section) when LocationTitle and AgeRangeTitle both are null or empty, similary i want hide the complete section

«TableStart:tblJobDetails»
    «QualTitle»
«TableEnd:tblJobDetails»

«TableStart:tblJobDetails»
    «QualSubTitle»    «StreamSubTitle»    «SpecializationSubTitle»
«TableEnd:tblJobDetails»

«TableStart:tblEducation»
    «Qualification»    «Stream»    «Specialization»
«TableEnd:tblEducation»

when QualTitle is null but the problem is the section is not an empty paragraph as it contain a table or colored header as you can see in the doc provided.
i am using executewithregions method

@SachinSingh Please try using MailMergeCleanupOptions.RemoveEmptyTableRows. If this does not help, you can try using IFieldMergingCallback and remove complete table or row that contain QualTitle:

DataSet ds = new DataSet();
ds.ReadXml(@"C:\Temp\data.xml");

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.FieldMergingCallback = new RemoveEmptyRowCallback();
doc.MailMerge.ExecuteWithRegions(ds);
doc.Save(@"C:\Temp\out.docx");
private class RemoveEmptyRowCallback : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldName == "QualTitle" && string.IsNullOrEmpty((string)args.FieldValue))
        {
            args.Field.Start.GetAncestor(NodeType.Row).Remove();
        }
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}

If this does not help, please create a simplified template, data source and expected output and attach them here. We will check them and provide you more information.

@alexey.noskov Thanks your code works perfectly for the QualTitle, QualSubTitle, and Qualification, it successfully removes these 3 rows
but I don’t know why is it not working for SkillsAndCompetencies, it is throwing error "Object reference not set to an instance of object, args.Field.Start.GetAncestor returned null " however the structure is the same for both.
Doc is attached for reference
Test.docx (41.3 KB)

@SachinSingh As I can see there is no the corresponding TableStart merge field at the beginning of your template:

Please try using the following modified template:
Test_modified.docx (37.8 KB)

I have tested it with the following dummy data and everything works as expected:

DataTable dt = new DataTable("tblJobDetails");
dt.Columns.Add("CompetenciesTitle");
dt.Columns.Add("SkillsAndCompetencies");
dt.Columns.Add("QualTitle");
dt.Columns.Add("QualSubTitle");
dt.Columns.Add("StreamSubTitle");
dt.Columns.Add("SpecializationSubTitle");
dt.Rows.Add("CompetenciesTitle", "", "QualTitle", "QualSubTitle", "StreamSubTitle", "SpecializationSubTitle");

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.MergeDuplicateRegions = true;
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.FieldMergingCallback = new RemoveEmptyRowCallback();
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save(@"C:\Temp\out.docx");

@alexey.noskov not working still getting the error args.Field.Start.GetAncestor returned null, when SkillsAndCompetencies is “”
Test_modified.docx (37.8 KB)

rivate class RemoveEmptyRowCallback : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        if ((args.FieldName == "QualTitle" ||args.FieldName=="SkillsAndCompetencies") && string.IsNullOrEmpty((string)args.FieldValue))
        {
            args.Field.Start.GetAncestor(NodeType.Row).Remove();
        }
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}

@SachinSingh Could you please attach your sample data source or create a simple console application that will allow us to reproduce the problem on our side? We will check the issue and provide you more information.

You can use the same data source you have provided above, the only difference is I am checking nulls for both QualTitle and SkillsAndCompetencies and it is not working for SkillsAndCompetencies, is it working for you?

private class RemoveEmptyRowCallback : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        if ((args.FieldName == "QualTitle" ||args.FieldName=="SkillsAndCompetencies") && string.IsNullOrEmpty((string)args.FieldValue))
        {
            args.Field.Start.GetAncestor(NodeType.Row).Remove();
        }
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}

@SachinSingh I have tested with your implementation of RemoveEmptyRowCallback and, unfortunately, still the problem is not reproducible on my side with the modified template. So, please create and provide a simple console application that will allow us to reproduce the problem. We will check the issue and provide you more information.

@alexey.noskov Can you share the output doc, is it removing the row containing skills and competencies, in my case, I am getting an error. I can’t create a console app as I don’t have aspose word on my PC, I am using it in an MVC app for my client.

@SachinSingh

You can install Aspose.Words from Nuget.

Here is output produced on my side:
out.docx (30.3 KB)

Thanks for your support the issue is now resolved, the issue was in the line

if ( e.DocumentFieldName == "SkillsAndCompetencies")
{
    // Add parsed HTML data to the document's body.
    DocumentBuilder builder = new DocumentBuilder(e.Document);
    builder.MoveToMergeField(e.DocumentFieldName);
    builder.InsertHtml((string)e.FieldValue, true);

    // Since we have already inserted the merged content manually,
    // we will not need to respond to this event by returning content via the "Text" property. 
    e.Text = string.Empty;
    }

now i modified it to call only when value is not null so that the code to remove the row work smoothly

@SachinSingh It is perfect that you managed to achieve what you need. Please feel free to ask in case of any other issues, we will glad to help you.