Substituting mergefield

Hi Aspose.Team!
How can I replace a mergefield of the form:
{MERGEFIELD Category : {MERGEFIELD ProductId}, description:{MERGEFIELD Description}}
With something that can be merge, like:
Category : {MERGEFIELD ProductId}, description:{MERGEFIELD Description}
Due to some misconception of my users, they end-up with a bunch of template employing such constructions.

Hi
Thanks for your interest in Aspose products. Try to use the following code snippet to solve you task.

Document doc = new Document(@"357_102667_nicson\in.doc");
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
NodeCollection separators = doc.GetChildNodes(NodeType.FieldSeparator, true);
// search starts of field that contains another fields
ArrayList startsList = new ArrayList();
foreach (FieldStart start in starts)
{
    Node node = start;
    while (node.NodeType != NodeType.FieldSeparator)
    {
        node = node.NextSibling;
        if (node.NodeType == NodeType.FieldStart)
        {
            startsList.Add(start);
            break;
        }
    }
}
// search separators of field that contains another fields
ArrayList separatorsList = new ArrayList();
foreach (FieldSeparator sep in separators)
{
    Node node = sep;
    if (node.PreviousSibling.NodeType == NodeType.FieldEnd)
    {
        separatorsList.Add(sep);
    }
}
// remove starts
foreach (FieldStart start in startsList)
{
    (start.NextSibling as Run).Text = (start.NextSibling as Run).Text.Replace("MERGEFIELD", "");
    start.Remove();
}
// remove separators, values and ends of fields
foreach (FieldSeparator sep in separatorsList)
{
    Node node = sep;
    while (node.NextSibling.NodeType != NodeType.FieldEnd)
    {
        node.NextSibling.Remove();
    }
    sep.NextSibling.Remove();
    sep.Remove();
}
// save document
doc.Save(@"357_102667_nicson\out.doc");

I hope that this will help you.
Best regards.

Thank you very much for your help.
It is working awesome, as always.
Thanks.

Hi Aspose team,
In the same order of idea, due to misconceptions, my user end-up with field like:
{MERGE Category}

How can I identify those field and replace them by:
{MERGEFIELD Category}
I guess it is very similar to what you gave me, but, I can’t figure how to adapt it.
Thanks for your help

Hi
Thanks for your inquiry. Could you please attach sample document here (only you and Aspose staff can download it)? I will investigate this and provide you more information.
Best regards.

Hi,
Thanks for your support.
I’ve attach a simplified example of what I’m encoutering, instead of a real template.
I think it will be clearer.

This really is nothing more than an incorrect syntax for the keyword MERGEFIELD.

I can always change this by hand, but If you have a programmatic way that will be a great time saver.
Both in editing time and teaching toward my users.

Hi
Thanks you for your inquiry. You can try using the following code.

Document doc = new Document("in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// get collection of fieldstarts
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
int count = starts.Count;
// loop through fieldstarts
for (int i = 0; i < count; i++)
{
    FieldStart start = (FieldStart)starts[i];
    // if field FieldType = 2 (I got this value experimentally)
    if (start.FieldType == (FieldType)2)
    {
        string code = string.Empty;
        Node current = start;
        // get code of field and remove old field
        while (current.NodeType != NodeType.FieldSeparator)
        {
            current = current.NextSibling;
            if (current.NodeType == NodeType.Run)
            {
                code += (current as Run).Text.Replace("DoMerge", "MERGEFIELD");
            }
            current.PreviousSibling.Remove();
        }
        while (current.NodeType != NodeType.FieldEnd)
        {
            current = current.NextSibling;
            current.PreviousSibling.Remove();
        }
        builder.MoveTo(current);
        // insert correct field.
        builder.InsertField(code, string.Empty);
        current.Remove();
    }
}
// execute mailMerge.
string[] names = { "COMPANY" };
string[] values = { "Aspose" };
doc.MailMerge.Execute(names, values);
doc.Save("out.doc");

I hope that this will help you.
Best regards.

Thanks, that’s exactly what I was looking for.
It works awesome.