-1

I've got and email template something like this.

" Good day personname,

I want to suggest a term.

Name and Surname: user will enter value

Rate: user will enter value

Company Name: User will enter value

Address: user will enter value

Suggested rate: user will enter value
"

This is an html email template and I need to retrieve the values the user has entered for each 'question'. I will then take these values and it's 'question' and create an html table from it.

Now, my issue is I need to find these values but I do not know of a good way to get the unknown string from the template/larger string.

  • 3
    Post sample HTML. I would suggest using `XDocument` or use HTML agility pack. – Michał Turczyn Jun 29 '20 at 05:59
  • 1
    See the upvoted answers in this question for suggestions: https://stackoverflow.com/questions/17252615/get-string-between-two-strings-in-a-string – nordenvall Jun 29 '20 at 06:05
  • 1
    A naive way would also be to use the built in [`string.Format`](https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netcore-3.1) method. For this you just have to make sure the values to be replaced are correctly marked (with `{n}`) – MindSwipe Jun 29 '20 at 06:14
  • @Mindswipe wrong direction I think. That's formatting and this is parsing – Caius Jard Jun 29 '20 at 06:53

1 Answers1

0

String parsing isn't so much about the unknown string; it's about the known string

You didn't post what your html email looks like but let us suppose it looks like this:

<p>Name and Surname: user will enter value</p>
<p>Rate: user will enter value</p>

As you can see it's quite a uniform structure. We could split on "<p>" and "</p>":

string parts = body.Split(new []{ "<p>", "</p>"}, StringSplitOptions.RemoveEmptyEntries);

And then we could loop through the resulting array looking for what we do know so we can retrieve what we don't know:

foreach(string part in parts)
{
  if(part.StartsWith("Name and Surname:"))
    name = part.Substring("Name and Surname:".Length).Trim();
  else if ....

}

Parsing this way isn't the most robust solution, but even robust solutions can be broken by relatively minor changes in the process. You could use HtmlAgilityPack to parse the HTML, look in every child element of the form for your known strings, change things so that it is looking for all three words "name" "and" "surname" individually and only deciding it's a name if all 3 are present .. and then someone will tweak it to say "firstname and lastname" and it breaks anyway. You have to decide how much effort to put into making a good job of a bad situation, and my advice would be "not much; do the minimum necessary while you set about replacing it with something proper, and increase the priority of this becomes a consistent pain point"

Really the entire system needs to be better engineered, web service, api calls etc but if this is what you're stuck with, and it's software preparing the email things will only really break if the remote software changes

Caius Jard
  • 72,509
  • 5
  • 49
  • 80