Aspose.Words.Examples.CSharp.Mail_Merge.ApplyCustomLogicToEmptyRegions.CreateDataSourceFromDocumentRegions C# (CSharp) Method

CreateDataSourceFromDocumentRegions() private static method

Returns a DataSet object containing a DataTable for the unmerged regions in the specified document. If regionsList is null all regions found within the document are included. If an ArrayList instance is present The only the regions specified in the list that are found in the document are added.
private static CreateDataSourceFromDocumentRegions ( Document doc, ArrayList regionsList ) : DataSet
doc Document
regionsList System.Collections.ArrayList
return System.Data.DataSet
        private static DataSet CreateDataSourceFromDocumentRegions(Document doc, ArrayList regionsList)
        {
            const string tableStartMarker = "TableStart:";
            DataSet dataSet = new DataSet();
            string tableName = null;

            foreach (string fieldName in doc.MailMerge.GetFieldNames())
            {
                if (fieldName.Contains(tableStartMarker))
                {
                    tableName = fieldName.Substring(tableStartMarker.Length);
                }
                else if (tableName != null)
                {
                    // Only add the table name as a new DataTable if it doesn't already exists in the DataSet.
                    if (dataSet.Tables[tableName] == null)
                    {
                        DataTable table = new DataTable(tableName);
                        table.Columns.Add(fieldName);

                        // We only need to add the first field for the handler to be called for the fields in the region.
                        if (regionsList == null || regionsList.Contains(tableName))
                        {
                            table.Rows.Add("FirstField");
                        }

                        dataSet.Tables.Add(table);
                    }
                    tableName = null;
                }
            }

            return dataSet;
        }
        // ExEnd:CreateDataSourceFromDocumentRegions