---
title: "Mail Merge with XML Data Source in C++"
---


```

## Purpose Summary

This page explains how to perform a mail merge using XML as the data source.

```

You can use a variety of data sources when performing a Mail Merge operation, including an XML file. XML can store repeating records and parent-child relationships. In C++, parse the file and implement [IMailMergeDataSource](https://reference.aspose.com/words/cpp/aspose.words.mailmerging/imailmergedatasource/) to provide those records to Aspose.Words.

## Advantages of XML As a Data Source

XML data sources are useful for storing data without the overhead of a database. They are useful for offline applications where users need to add, edit, and delete data when they cannot connect to a database.

Since XML stores data in plain text format, the storage is platform-independent. Thus, data can be exported, imported, or moved between applications.

## Fill a Merge Template from XML

The following XML contains the data needed to fill a merge template. Download [Mail merge data - Customers.xml](https://github.com/aspose-words/Aspose.Words-for-C/blob/37ec0d81af266736c566bd082e99321c5da80e56/Examples/Data/Mail%20merge%20data%20-%20Customers.xml) and place it in `MyDir`, the examples' data directory.

```xml
<?xml version="1.0" encoding="utf-8"?>
<customers>
  <customer cName="John Ben Jan" cID="1" cDomain="History" cCity="Boston"/>
  <customer cName="Lisa Lane" cID="2" cDomain="Chemistry" cCity="LA"/>
  <customer cName="Dagomir Zits" cID="3" cDomain="Heraldry" cCity="Milwaukee"/>
  <customer cName="Sara Careira Santy" cID="4" cDomain="IT" cCity="Miami"/>
</customers>
```

Each `customer` element is one record. The example builds a Word template with a region bounded by the merge fields `TableStart:customer` and `TableEnd:customer`. The merge fields inside the region are `cName` and `cCity`, which match the XML attribute names. You can use the same template for different data sources that provide these fields.

The C++ example uses the XML reader provided with the Aspose.Words C++ SDK and selects the customer records with XPath. The data source starts before the first record. [MoveNext](https://reference.aspose.com/words/cpp/aspose.words.mailmerging/imailmergedatasource/movenext/) advances to the next customer, and [GetValue](https://reference.aspose.com/words/cpp/aspose.words.mailmerging/imailmergedatasource/getvalue/) returns the attribute requested by Mail Merge.

```cpp
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
class CustomerDataSource : public IMailMergeDataSource
{
public:
    explicit CustomerDataSource(const SharedPtr<System::Xml::XmlNodeList>& customers)
        : customers_(customers)
    {
    }

    String get_TableName() override { return u"customer"; }

    bool MoveNext() override
    {
        if (next_ == customers_->get_Count())
            return false;
        auto node = customers_->idx_get(next_++);
        current_ = System::DynamicCast<System::Xml::XmlElement>(node);
        return true;
    }

    bool GetValue(String fieldName, SharedPtr<System::Object>& fieldValue) override
    {
        if (current_->HasAttribute(fieldName))
        {
            fieldValue = System::ObjectExt::Box<String>(current_->GetAttribute(fieldName));
            return true;
        }
        fieldValue.reset();
        return false;
    }

    SharedPtr<IMailMergeDataSource> GetChildDataSource(String) override
    {
        return nullptr;
    }

private:
    SharedPtr<System::Xml::XmlNodeList> customers_;
    SharedPtr<System::Xml::XmlElement> current_;
    int next_ = 0;
};

auto xml = MakeObject<System::Xml::XmlDocument>();
xml->Load(System::Xml::XmlReader::Create(MyDir + u"Mail merge data - Customers.xml"));
auto customers = xml->SelectNodes(u"/customers/customer");

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u"MERGEFIELD TableStart:customer");
builder->InsertField(u"MERGEFIELD cName");
builder->Write(u", ");
builder->InsertField(u"MERGEFIELD cCity");
builder->InsertField(u"MERGEFIELD TableEnd:customer");

doc->get_MailMerge()->ExecuteWithRegions(MakeObject<CustomerDataSource>(customers));
doc->Save(ArtifactsDir + u"WorkingWithXmlData.XmlMailMerge.docx");
```

[ExecuteWithRegions](https://reference.aspose.com/words/cpp/aspose.words.mailmerging/mailmerge/executewithregions/) repeats the region for the four customers. The merged document contains the following lines:

```text
John Ben Jan, Boston
Lisa Lane, LA
Dagomir Zits, Milwaukee
Sara Careira Santy, Miami
```

To merge nested XML records, implement [GetChildDataSource](https://reference.aspose.com/words/cpp/aspose.words.mailmerging/imailmergedatasource/getchilddatasource/) to return a data source for the child elements of the current record. If the template has multiple top-level regions, use [IMailMergeDataSourceRoot](https://reference.aspose.com/words/cpp/aspose.words.mailmerging/imailmergedatasourceroot/) to provide the appropriate data source for each top-level region.

## See Also

- [Types of Mail Merge Operation](/words/cpp/types-of-mail-merge-operations/)
- [Nested Mail Merge with Regions](/words/cpp/nested-mail-merge-with-regions/)
