Search code examples
c#dynamic-html

dynamically change the html table tag width which is in string


I am doing a CLR procedure of sending mail, I have composed the html into my string , After binding some dynamic values I have been able to send mail.

But Now the issue is , I am getting a string which containing the HTML, So I want to find the first <table> and then change its width. it.[Due to large width the template is disturbing]. This is the body of the email.

 string StrHtml =" <table cellspacing='1' cellpadding='10' border='0'
style='width:880px'></table>"

I want to change the style='width:880px' to style='width:550px'

I am doing this code only in class library. what is the best way to do this?

My Code is :

string ImgPath = string.Empty;
ImgPath = Convert.ToString(ds.Tables[0].Rows[i]["GroupMessage"]);
string pattern = string.Empty;
pattern = System.Text.RegularExpressions.Regex.(ImgPath, "(<table.*?>.*</table>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Groups[1].Value;  

MailMessage message = new MailMessage();
message.AlternateViews.Add(htmlMail);
message.Body = ImgPath ; //
message.IsBodyHtml = true;
//Other mail sending code here.....

Solution

  • I am getting the html string in below format and I want to get only the attribute value of style="width: 880px" , which is always changing.[This is only work if the width is mention in style tag. And that is the my case.]

    ImgPath ==>

     <table border="0" cellpadding="10" cellspacing="1" style="width: 880px">
        // some other code goes here
        </html>
    

    Answer ==> I used regular expression in my class, [want to make DLL file from that class]

    string StrStyTag = string.Empty;
    StrStyTag = System.Text.RegularExpressions.Regex.Match(ImgPath, "<table.+?style=(.+?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Groups[1].Value;
    

    I am getting the value as ==> "width: 880px", So after doing some string manipulations I make the 880px to fix 550px.

    Thanks for all those who help me to resolved it.