Hi
Thanks for your request. No, your code is not correct, if you need to fill the document with data, which has parent-child relationships. In this case, you should use IMailMergeDataSource.GetChildDataSource.
http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.reporting.imailmergedatasource.getchilddatasource.html
For example, see the following code:
List properties= new List();
for(int i=0; i<10; i++)
{
Property prop = new Property(string.Format(“property_{0}”, i));
// Add few buyers.
for(int j=0; j<5; j++)
{
prop.Buyers.Add(new Buyer(string.Format(“firstName_{0}-{1}”, i, j),
string.Format(“lastName_{0}-{1}”, i, j)));
}
properties.Add(prop);
}
// Create datasource.
PropertiesDataSource ds = new PropertiesDataSource(properties);
// Open template.
Document doc = new Document(@“Test001\in.doc”);
// Execute mail merge with regions.
doc.MailMerge.ExecuteWithRegions(ds);
// Save output document.
doc.Save(@“Test001\out.doc”);
====================================================================
///
/// Properties datasource.
///
private class PropertiesDataSource : IMailMergeDataSource
{
public PropertiesDataSource(List properties)
{
mProperties = properties;
// When the data source is initialized, it must be positioned before the first record.
mRecordIndex = -1;
}
public bool MoveNext()
{
if (!IsEof)
mRecordIndex++;
return (!IsEof);
}
public bool GetValue(string fieldName, out object fieldValue)
{
switch (fieldName)
{
case “Name”:
fieldValue = mProperties[mRecordIndex].Name;
return true;
default:
fieldValue = null;
return false;
}
}
public IMailMergeDataSource GetChildDataSource(string tableName)
{
switch (tableName)
{
case “Buyers”:
return new BuyersDataSource(mProperties[mRecordIndex].Buyers);
default:
return null;
}
}
public string TableName
{
get { return “Properties”; }
}
private bool IsEof
{
get { return (mRecordIndex >= mProperties.Count); }
}
private int mRecordIndex;
private List mProperties;
}
///
/// Buyers datasource.
///
private class BuyersDataSource : IMailMergeDataSource
{
public BuyersDataSource(List buyers)
{
mBuyers = buyers;
// When the data source is initialized, it must be positioned before the first record.
mRecordIndex = -1;
}
public bool MoveNext()
{
if (!IsEof)
mRecordIndex++;
return (!IsEof);
}
public bool GetValue(string fieldName, out object fieldValue)
{
switch (fieldName)
{
case “FirstName”:
fieldValue = mBuyers[mRecordIndex].FirstName;
return true;
case “LastName”:
fieldValue = mBuyers[mRecordIndex].LastName;
return true;
default:
fieldValue = null;
return false;
}
}
public IMailMergeDataSource GetChildDataSource(string tableName)
{
return null;
}
public string TableName
{
get { return “Buyers”; }
}
private bool IsEof
{
get { return (mRecordIndex >= mBuyers.Count); }
}
private int mRecordIndex;
private List mBuyers;
}
=============================================================================
// These are dummy classes.
private class Property
{
public Property(string name)
{
mName = name;
mBuyers = new List();
}
public string Name
{
get { return mName; }
}
public List Buyers
{
get { return mBuyers; }
}
private string mName;
private List mBuyers;
}
private class Buyer
{
public Buyer(string firstName, string lastName)
{
mFirstName = firstName;
mLastName = lastName;
}
public string FirstName
{
get { return mFirstName; }
}
public string LastName
{
get { return mLastName; }
}
private string mFirstName;
private string mLastName;
}
Hope this helps.
Best regards.