happysmile
happysmile

Reputation: 7777

How to read an HTML file and replace with our characters

I have an HTML file template which is used for mailing purpose here in the template.

Hi XXX,
Thanks for joing in the Club, your ClubcardNo is : XXXXX

Thanks

Here now in the code I need to replace this XXXX with name and clubcard no:

name:Kiran
clubcard no: 23453

How can I get this done?

Upvotes: 2

Views: 10083

Answers (5)

Reza Nabiloo
Reza Nabiloo

Reputation: 73

You can mark your special words. For example:

Your code in HTML file:

<p>  welcome #Name #Family </p> 

In C# use this code:

StreamReader sr =new StreamReader("Path of your file");
string s = sr.ReadToEnd();
s.Replace("#name","123456").Replace("#Family","123456");
sr.Close();}

Upvotes: 5

Arion
Arion

Reputation: 31239

var str=@"Hi XXX,
                Thanks for joing in the Club, your ClubcardNo is : XXXXX

                Thanks";

str.Replace("XXXXX","23453").Replace("XXX","Kiran");

Upvotes: 1

Damir Arh
Damir Arh

Reputation: 17855

I'd suggest you try using a templating engine like RazorEngine instead. It will give you more flexibility and robustness, while still being simple to use.

Upvotes: -2

egres
egres

Reputation: 284

Try following:

string output = Regex.Replace(input, "XXX", "Kiran");

where input is your html template as a string.

Upvotes: 0

Daniel Casserly
Daniel Casserly

Reputation: 3500

If you have this as a string value. (ie you read it in as a string from the file) then you can just do

String s = s.Replace("XXXXX", "23453");

Where s is the string that you retrieved from the file and the number is an example. I'm sure you won't hardcode them like that.

Upvotes: 2

Related Questions