We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Finding text between tag

Hi,
I have some requirement to get text from one file and copy in to other file.
for example
source document contains tag like
i need to get value for FIRSTNAME tag thats is “vidhya”
With this value i need to replace tag present in destination file.
Destination file may contain tag like <%FIRSTNAME%>
so in destination file <%FIRSTNAME%> should be replaced with vidhya.
How i can do using Aspose.

Hi
Thanks for your interest in Aspose.Words. I think that you can achieve this using regular expressions and ReplaceEvaluator. For example see the following code and attached documents.

// Collection of Key Value pairs
Hashtable keyValueCollection = new Hashtable();
public void TestInsertValues()
{
    // Open source document
    Document srcDoc = new Document(@"468_110179_vidhya.thawale\in.doc");
    // Create regecx for searching values in document
    Regex regexFindValue = new Regex(@"<(?\S+):(?.*?)>");
    // Find values
    srcDoc.Range.Replace(regexFindValue, new ReplaceEvaluator(ReplaceActionFindValues), true);
    // Open destination document
    Document tempDoc = new Document(@"468_110179_vidhya.thawale\temp.doc");
    // Create regex for searching placeholders
    Regex regexFindPlaceholder = new Regex(@"<%(?\S+)%>");
    // Replace placeholder with value
    tempDoc.Range.Replace(regexFindPlaceholder, new ReplaceEvaluator(ReplaceActionFindPlaceholder), true);
    // Save output
    tempDoc.Save(@"468_110179_vidhya.thawale\out.doc");
}
ReplaceAction ReplaceActionFindValues(object sender, ReplaceEvaluatorArgs e)
{
    // Add pair to collection
    keyValueCollection.Add(e.Match.Groups["name"].Value, e.Match.Groups["value"].Value);
    return ReplaceAction.Skip;
}
ReplaceAction ReplaceActionFindPlaceholder(object sender, ReplaceEvaluatorArgs e)
{
    if (keyValueCollection.ContainsKey(e.Match.Groups["name"].Value))
    {
        // Replace placeholder with value
        e.Replacement = keyValueCollection[e.Match.Groups["name"].Value].ToString();
        return ReplaceAction.Replace;
    }
    else
    {
        return ReplaceAction.Skip;
    }
}

I hope that this will help you.
Best regards.