0

As an introduction, I have created a document using MS Word and then saved as html document. From C# I am building an unordered html list (using MS Word format) and then add it to the html document by replacing a specific tag.

I have below string variable unorderedHtmlList initially initialized to empty string. Then I am concatenating html string and replacing some tags enclosed by "[[" and "]]" characters. For some reason when I apply the Replace it is not replacing the items [[fieldName]] and [[fieldValue]] by the new values. See code below:

string unorderedHtmlList = string.Empty;

foreach (System.Data.DataRow row in myDataTable.Rows)
{
    string name = row["fieldName"].ToString();               
    string value = row["fieldValue"].ToString();

    unorderedHtmlList += "<p style='margin-left:36.0pt;text-align:justify;text-indent:-18.0pt;" +
                        "line-height:125%;mso-list:l1 level1 lfo3'><![if !supportLists]><span" +
                        "style='font-size:10.5pt;line-height:125%;font-family:\"Arial\",sans-serif;" +                                        
                        "mso-fareast-font-family:Arial;color:#222222'><span" +
                        "style='mso-list:Ignore'>-<span style='font:7.0pt \"Times New Roman\"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "</span></span></span><![endif]><span style='font-size:10.5pt;" +
                        "line-height:125%;font-family:\"Arial\",sans-serif;color:#222222'>[[fieldName]]" +
                        "</span><span style='font-size:10.5pt;line-height:125%;font-family:" +
                        "\"Helvetica\",sans-serif;color:#222222'>[[fieldValue]]</span><span" +
                        "style='font-size:10.5pt;line-height:125%;font-family:\"Arial\",sans-serif;" +
                        "color:#222222'><o:p></o:p></span></p>".Replace("[[fieldName]]", name).Replace("[[fieldValue]]", value);
}

Any ideas why Replace is not working?

7
  • What does it replace it with? Or does it leave the original value in place? Commented Apr 9, 2020 at 12:11
  • 2
    Put parenthesys around the string Ie: unorderedHtmlList += (.....).Replace(....) Probably there is some rules about constants strings, Pretty sure there is a duplicate somewhere Commented Apr 9, 2020 at 12:15
  • 1
    Looking at the IL generated it is clear now. The replace happens only on the last string then everything is concatenated together but the replace has already been called Commented Apr 9, 2020 at 12:24
  • 1
    @Steve, I don't want to look like it, I posted 9 seconds earlier, I'm not a borrower :) Commented Apr 9, 2020 at 12:26
  • 1
    Thanks. Checking the IL? Guaranteed to see :) Commented Apr 9, 2020 at 12:27

1 Answer 1

2

You are concatanating the string and the replace operation is executed only on the last part.

"color:#222222'><o:p></o:p></span></p>".Replace("[[fieldName]]", name).Replace("[[fieldValue]]", value);

Try this:

        unorderedHtmlList += ("<p style='margin-left:36.0pt;text-align:justify;text-indent:-18.0pt;" +
                            "line-height:125%;mso-list:l1 level1 lfo3'><![if !supportLists]><span" +
                            "style='font-size:10.5pt;line-height:125%;font-family:\"Arial\",sans-serif;" +
                            "mso-fareast-font-family:Arial;color:#222222'><span" +
                            "style='mso-list:Ignore'>-<span style='font:7.0pt \"Times New Roman\"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                            "</span></span></span><![endif]><span style='font-size:10.5pt;" +
                            "line-height:125%;font-family:\"Arial\",sans-serif;color:#222222'>[[fieldName]]" +
                            "</span><span style='font-size:10.5pt;line-height:125%;font-family:" +
                            "\"Helvetica\",sans-serif;color:#222222'>[[fieldValue]]</span><span" +
                            "style='font-size:10.5pt;line-height:125%;font-family:\"Arial\",sans-serif;" +
                            "color:#222222'><o:p></o:p></span></p>").Replace("[[fieldName]]", name).Replace("[[fieldValue]]", value);         
Sign up to request clarification or add additional context in comments.

1 Comment

Ops, sorry. You are right this was being applied to the last string not the full string. I forgot the parentheses. What a silly mistake! Thanks!

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.