Вопрос

I have a program that sends emails utilizing templates via a web service. To test the templates, I made a simple program that reads the templates, fills it up with dummy value and send it. The problem is that the templates have different 'fill in' variable names. So what I want to do is open the template, make a list of the variables and then fill them with dummy text.

Right no I have something like:

StreamReader SR = new StreamReader(myPath);
.... //Email code here
Msg.Body = SR.ReadToEnd();
SR.Close();

Msg.Body = Msg.Body.Replace(%myFillInVariable%, "Test String");
....

So I'm thinking, opening the template, search for values in between "%" and put them in an ArrayList, then do the Msg.Body = SR.ReadToEnd(); part. Loop the ArrayList and do the Replace part using the value of the Array.

What I can't find is how to read the value between the % tags. Any suggestions on what method to use will be greatly appreciated.

Thanks,

MORE DETAILS:

Sorry if I wasn't clear. I'm passing the name of the TEMPLATE to the script from a drop down. I might have a few dozen Templates and they all have different %VariableToBeReplace%. So that's is why I want to read the Template with the StreamReader, find all the %value names%, put them into an array AND THEN fill them up - which I already know how to do. It's getting the the name of what I need to replace in code which I don't know what to do.

Это было полезно?

Решение

I am not sure on your question either but here is a sample of how to do the replacement.

You can run and play with this example in LinqPad.

Copy this content into a file and change the path to what you want. Content:

Hello %FirstName% %LastName%,

We would like to welcome you and your family to our program at the low cost of   %currentprice%. We are glad to offer you this %Service%

Thanks,
Some Person

Code:

var content = string.Empty;
using(var streamReader = new StreamReader(@"C:\EmailTemplate.txt"))
{
    content = streamReader.ReadToEnd();
}

var matches = Regex.Matches(content, @"%(.*?)%", RegexOptions.ExplicitCapture);

var extractedReplacementVariables = new List<string>(matches.Count);
foreach(Match match in matches)
{
    extractedReplacementVariables.Add(match.Value);
}

extractedReplacementVariables.Dump("Extracted KeyReplacements");

//Do your code here to populate these, this part is just to show it still works
//Modify to meet your needs
var replacementsWithValues = new Dictionary<string, string>(extractedReplacementVariables.Count);
for(var i = 0; i < extractedReplacementVariables.Count; i++)
{
    replacementsWithValues.Add(extractedReplacementVariables[i], "TestValue" + i);
}

content.Dump("Template before Variable Replacement");

foreach(var key in replacementsWithValues.Keys)
{
    content = content.Replace(key, replacementsWithValues[key]);
}

content.Dump("Template After Variable Replacement");

Result from LinqPad: LinqPad Results

Другие советы

I am not really sure that I understood your question but, you can try to put on the first line of the template your 'fill in variable'.

Something like:

StreamReader SR = new StreamReader(myPath);
String fill_in_var=SR.ReadLine();
String line;
while((line = SR.ReadLine()) != null)
{
    Msg.Body+=line;
}
SR.Close();

Msg.Body = Msg.Body.Replace(fill_in_var, "Test String");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top