0

I need to create this html string :

<table cellspacing="2" style="width:100%">
    <tr height="30" nowrap="NOWRAP" width="200" bgcolor="#f4f4f4">
      <th>Product</th>
      <th>Category</th> 
      <th>Price</th>
   </tr>
   <tr>
      <td>Replace</td>
      <td>Replace</td>
      <td>Replace</td>
  </tr>
</table>

And I have a List of items that I intend to replace. I Was trying to do with StringBuilder but it wasn't working!

3
  • 2
    What does "it was not working" mean? Did your application crash? Did you get an unexpected result? What exactly happened? Also you need to supply the code you have so far, you can replace / build a string in many different ways and until you clarify how you are doing this no one can answer your question. Commented Jul 8, 2017 at 11:03
  • When I tried to create something like cellspacing="2" it wasn't working. And the "td's" has to be replaced by the items from a List. Commented Jul 8, 2017 at 11:06
  • Maybe its worth a look into the RazorEngine Commented Jul 8, 2017 at 11:26

1 Answer 1

2

You can use the @ for verbatim string and double the " marks to escape them. Also you can use string.Format with {} placeholders to inject values although they are not html encoded so if you have characters like < in the string they would need to be escaped. This is 1 variant of many and without you sharing what you have so far it is as good as any answer you might get.

var html = string.Format(
@"<table cellspacing=""2"" style=""width:100%"">
    <tr height=""30"" nowrap=""NOWRAP"" width=""200"" bgcolor=""#f4f4f4"">
      <th>Product</th>
      <th>Category</th> 
      <th>Price</th>
   </tr>
   <tr>
      <td>{0}</td>
      <td>{1}</td>
      <td>{2}</td>
  </tr>
</table>", listItem[0], listItem[1], listItem[2]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was exactly what I need!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.